blob: 84b6af776883d806cfd024040df9a8525e63fc3d [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,
42 Callback* callback = nullptr) :
43 Watch(),
44 index(watchIndex), cb(callback), alreadyRan(false)
45 {
46 }
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 */
82 virtual void propertiesChanged(sdbusplus::message::message&,
83 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 */
93 virtual void interfacesAdded(sdbusplus::message::message& 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;
Brad Bishop4b916f12017-05-23 18:06:38 -0400104};
105
106/** @class PropertyWatchOfType
107 * @brief Type specific logic for PropertyWatch.
108 *
109 * @tparam DBusInterfaceType - DBus access delegate.
110 * @tparam T - The type of the properties being watched.
111 */
112template <typename T, typename DBusInterfaceType>
113class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
114{
Brad Bishopd1eac882018-03-29 10:34:05 -0400115 public:
116 PropertyWatchOfType() = default;
117 PropertyWatchOfType(const PropertyWatchOfType&) = delete;
118 PropertyWatchOfType(PropertyWatchOfType&&) = default;
119 PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete;
120 PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default;
121 ~PropertyWatchOfType() = default;
Matthew Barthefe01582019-09-09 15:22:37 -0500122 PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback,
123 Filters* filterOps = nullptr) :
Matthew Barthae786ef2019-09-04 15:46:13 -0500124 PropertyWatch<DBusInterfaceType>(watchIndex, &callback),
125 filterOps(filterOps)
Brad Bishopd1eac882018-03-29 10:34:05 -0400126 {
127 }
Matthew Barthefe01582019-09-09 15:22:37 -0500128 PropertyWatchOfType(const PropertyIndex& watchIndex,
129 Filters* filterOps = nullptr) :
Matthew Barthae786ef2019-09-04 15:46:13 -0500130 PropertyWatch<DBusInterfaceType>(watchIndex, nullptr),
131 filterOps(filterOps)
Brad Bishopd1eac882018-03-29 10:34:05 -0400132 {
133 }
Brad Bishopfccdc392017-05-22 21:11:09 -0400134
Brad Bishopd1eac882018-03-29 10:34:05 -0400135 /** @brief PropertyMatch implementation for PropertyWatchOfType.
136 *
137 * @param[in] busName - The busname hosting the interface to query.
138 * @param[in] path - The path of the interface to query.
139 * @param[in] interface - The interface to query.
140 */
141 void updateProperties(const std::string& busName, const std::string& path,
142 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400143
Brad Bishopd1eac882018-03-29 10:34:05 -0400144 /** @brief PropertyMatch implementation for PropertyWatchOfType.
145 *
146 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
147 * message.
148 * @param[in] path - The path associated with the message.
149 * @param[in] interface - The interface associated with the message.
150 */
151 void propertiesChanged(sdbusplus::message::message& msg,
152 const std::string& path,
153 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400154
Brad Bishopd1eac882018-03-29 10:34:05 -0400155 /** @brief DBus agnostic implementation of interfacesAdded.
156 *
157 * @param[in] path - The path of the properties that changed.
158 * @param[in] interface - The interface of the properties that
159 * changed.
160 * @param[in] properites - The properties that changed.
161 */
162 void propertiesChanged(const std::string& path,
163 const std::string& interface,
164 const PropertiesChanged<T>& properties);
Brad Bishop4b916f12017-05-23 18:06:38 -0400165
Brad Bishopd1eac882018-03-29 10:34:05 -0400166 /** @brief PropertyMatch implementation for PropertyWatchOfType.
167 *
168 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
169 * message.
170 */
171 void interfacesAdded(sdbusplus::message::message& msg) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400172
Brad Bishopd1eac882018-03-29 10:34:05 -0400173 /** @brief DBus agnostic implementation of interfacesAdded.
174 *
175 * @param[in] path - The path of the added interfaces.
176 * @param[in] interfaces - The added interfaces.
177 */
178 void interfacesAdded(const std::string& path,
179 const InterfacesAdded<T>& interfaces);
Matthew Barthae786ef2019-09-04 15:46:13 -0500180
181 private:
Matthew Barthefe01582019-09-09 15:22:37 -0500182 /** @brief Optional filter operations to perform on property changes. */
183 Filters* const filterOps;
Brad Bishop4b916f12017-05-23 18:06:38 -0400184};
185
186} // namespace monitoring
187} // namespace dbus
188} // namespace phosphor