blob: 57eda6f2906e1fe12ce6cc8980e46b344bcff9d4 [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) :
Patrick Williamseab4f8c2024-08-16 15:20:10 -040044 Watch(), index(watchIndex), cb(callback), alreadyRan(false),
Lei YU98d64622022-05-24 19:11:23 +080045 ignoreStartCallback(ignoreStartCallback)
George Liu3fe976c2022-06-21 09:37:04 +080046 {}
Brad Bishop4b916f12017-05-23 18:06:38 -040047
Brad Bishopd1eac882018-03-29 10:34:05 -040048 /** @brief Start the watch.
49 *
50 * Watch start interface implementation for PropertyWatch.
51 */
52 void start() override;
Brad Bishop4b916f12017-05-23 18:06:38 -040053
Brad Bishopd1eac882018-03-29 10:34:05 -040054 /** @brief Run the watch callback method.
55 *
56 * Watch callback interface implementation for PropertyWatch.
57 */
58 void callback(Context ctx) override;
Brad Bishopce4fbe12017-06-06 23:58:09 -040059
Brad Bishopd1eac882018-03-29 10:34:05 -040060 /** @brief Update properties.
61 *
62 * Subclasses to query the properties specified by the index
63 * and update the cache.
64 *
65 * @param[in] busName - The busname hosting the interface to query.
66 * @param[in] path - The path of the interface to query.
67 * @param[in] interface - The interface to query.
68 */
69 virtual void updateProperties(const std::string& busName,
70 const std::string& path,
71 const std::string& interface) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040072
Brad Bishopd1eac882018-03-29 10:34:05 -040073 /** @brief Dbus signal callback for PropertiesChanged.
74 *
75 * Subclasses to update the cache.
76 *
77 * @param[in] message - The org.freedesktop.DBus.PropertiesChanged
78 * message.
79 * @param[in] path - The path associated with the message.
80 * @param[in] interface - The interface associated with the message.
81 */
Patrick Williams413a4852022-07-22 19:26:52 -050082 virtual void propertiesChanged(sdbusplus::message_t&,
Brad Bishopd1eac882018-03-29 10:34:05 -040083 const std::string& path,
84 const std::string& interface) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040085
Brad Bishopd1eac882018-03-29 10:34:05 -040086 /** @brief Dbus signal callback for InterfacesAdded.
87 *
88 * Subclasses to update the cache.
89 *
90 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
91 * message.
92 */
Patrick Williams413a4852022-07-22 19:26:52 -050093 virtual void interfacesAdded(sdbusplus::message_t& msg) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040094
Brad Bishopd1eac882018-03-29 10:34:05 -040095 protected:
96 /** @brief Property names and their associated storage. */
97 const PropertyIndex& index;
Brad Bishop4b916f12017-05-23 18:06:38 -040098
Brad Bishopd1eac882018-03-29 10:34:05 -040099 /** @brief Optional callback method. */
100 Callback* const cb;
Brad Bishop4b916f12017-05-23 18:06:38 -0400101
Brad Bishopd1eac882018-03-29 10:34:05 -0400102 /** @brief The start method should only be invoked once. */
103 bool alreadyRan;
Lei YU98d64622022-05-24 19:11:23 +0800104
105 /** @brief Ignore callback on start */
106 bool ignoreStartCallback;
Brad Bishop4b916f12017-05-23 18:06:38 -0400107};
108
109/** @class PropertyWatchOfType
110 * @brief Type specific logic for PropertyWatch.
111 *
112 * @tparam DBusInterfaceType - DBus access delegate.
113 * @tparam T - The type of the properties being watched.
114 */
115template <typename T, typename DBusInterfaceType>
116class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
117{
Brad Bishopd1eac882018-03-29 10:34:05 -0400118 public:
119 PropertyWatchOfType() = default;
120 PropertyWatchOfType(const PropertyWatchOfType&) = delete;
121 PropertyWatchOfType(PropertyWatchOfType&&) = default;
122 PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete;
123 PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default;
124 ~PropertyWatchOfType() = default;
Matthew Barthefe01582019-09-09 15:22:37 -0500125 PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback,
Lei YU98d64622022-05-24 19:11:23 +0800126 bool ignoreStartCallback = false,
Matthew Barthefe01582019-09-09 15:22:37 -0500127 Filters* filterOps = nullptr) :
Lei YU98d64622022-05-24 19:11:23 +0800128 PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback,
129 &callback),
Matthew Barthae786ef2019-09-04 15:46:13 -0500130 filterOps(filterOps)
George Liu3fe976c2022-06-21 09:37:04 +0800131 {}
George Liuecef1192022-07-07 08:59:12 +0800132 explicit PropertyWatchOfType(const PropertyIndex& watchIndex,
133 bool ignoreStartCallback = false,
134 Filters* filterOps = nullptr) :
Lei YU98d64622022-05-24 19:11:23 +0800135 PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback,
136 nullptr),
Matthew Barthae786ef2019-09-04 15:46:13 -0500137 filterOps(filterOps)
George Liu3fe976c2022-06-21 09:37:04 +0800138 {}
Brad Bishopfccdc392017-05-22 21:11:09 -0400139
Brad Bishopd1eac882018-03-29 10:34:05 -0400140 /** @brief PropertyMatch implementation for PropertyWatchOfType.
141 *
142 * @param[in] busName - The busname hosting the interface to query.
143 * @param[in] path - The path of the interface to query.
144 * @param[in] interface - The interface to query.
145 */
146 void updateProperties(const std::string& busName, const std::string& path,
147 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400148
Brad Bishopd1eac882018-03-29 10:34:05 -0400149 /** @brief PropertyMatch implementation for PropertyWatchOfType.
150 *
151 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
152 * message.
153 * @param[in] path - The path associated with the message.
154 * @param[in] interface - The interface associated with the message.
155 */
Patrick Williams413a4852022-07-22 19:26:52 -0500156 void propertiesChanged(sdbusplus::message_t& msg, const std::string& path,
Brad Bishopd1eac882018-03-29 10:34:05 -0400157 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400158
Brad Bishopd1eac882018-03-29 10:34:05 -0400159 /** @brief DBus agnostic implementation of interfacesAdded.
160 *
161 * @param[in] path - The path of the properties that changed.
162 * @param[in] interface - The interface of the properties that
163 * changed.
Manojkiran Eda0c1e0242024-06-17 12:02:59 +0530164 * @param[in] properties - The properties that changed.
Brad Bishopd1eac882018-03-29 10:34:05 -0400165 */
166 void propertiesChanged(const std::string& path,
167 const std::string& interface,
168 const PropertiesChanged<T>& properties);
Brad Bishop4b916f12017-05-23 18:06:38 -0400169
Brad Bishopd1eac882018-03-29 10:34:05 -0400170 /** @brief PropertyMatch implementation for PropertyWatchOfType.
171 *
172 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
173 * message.
174 */
Patrick Williams413a4852022-07-22 19:26:52 -0500175 void interfacesAdded(sdbusplus::message_t& msg) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400176
Brad Bishopd1eac882018-03-29 10:34:05 -0400177 /** @brief DBus agnostic implementation of interfacesAdded.
178 *
179 * @param[in] path - The path of the added interfaces.
180 * @param[in] interfaces - The added interfaces.
181 */
182 void interfacesAdded(const std::string& path,
183 const InterfacesAdded<T>& interfaces);
Matthew Barthae786ef2019-09-04 15:46:13 -0500184
185 private:
Matthew Barthefe01582019-09-09 15:22:37 -0500186 /** @brief Optional filter operations to perform on property changes. */
187 Filters* const filterOps;
Brad Bishop4b916f12017-05-23 18:06:38 -0400188};
189
190} // namespace monitoring
191} // namespace dbus
192} // namespace phosphor