blob: ecb8e9c50528a114d2eac4eb574b911d589f4cbe [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
14namespace phosphor
15{
16namespace dbus
17{
18namespace monitoring
19{
20
Brad Bishopfccdc392017-05-22 21:11:09 -040021class Callback;
22
Brad Bishop4b916f12017-05-23 18:06:38 -040023/** @class PropertyWatch
24 * @brief Type agnostic, factored out logic for property watches.
25 *
26 * A property watch maintains the state of one or more DBus properties
27 * as specified by the supplied index.
28 */
Patrick Venture3d6d3182018-08-31 09:33:09 -070029template <typename DBusInterfaceType>
30class PropertyWatch : public Watch
Brad Bishop4b916f12017-05-23 18:06:38 -040031{
Brad Bishopd1eac882018-03-29 10:34:05 -040032 public:
33 PropertyWatch() = delete;
34 PropertyWatch(const PropertyWatch&) = delete;
35 PropertyWatch(PropertyWatch&&) = default;
36 PropertyWatch& operator=(const PropertyWatch&) = delete;
37 PropertyWatch& operator=(PropertyWatch&&) = default;
38 virtual ~PropertyWatch() = default;
39 PropertyWatch(const PropertyIndex& watchIndex,
40 Callback* callback = nullptr) :
41 Watch(),
42 index(watchIndex), cb(callback), alreadyRan(false)
43 {
44 }
Brad Bishop4b916f12017-05-23 18:06:38 -040045
Brad Bishopd1eac882018-03-29 10:34:05 -040046 /** @brief Start the watch.
47 *
48 * Watch start interface implementation for PropertyWatch.
49 */
50 void start() override;
Brad Bishop4b916f12017-05-23 18:06:38 -040051
Brad Bishopd1eac882018-03-29 10:34:05 -040052 /** @brief Run the watch callback method.
53 *
54 * Watch callback interface implementation for PropertyWatch.
55 */
56 void callback(Context ctx) override;
Brad Bishopce4fbe12017-06-06 23:58:09 -040057
Brad Bishopd1eac882018-03-29 10:34:05 -040058 /** @brief Update properties.
59 *
60 * Subclasses to query the properties specified by the index
61 * and update the cache.
62 *
63 * @param[in] busName - The busname hosting the interface to query.
64 * @param[in] path - The path of the interface to query.
65 * @param[in] interface - The interface to query.
66 */
67 virtual void updateProperties(const std::string& busName,
68 const std::string& path,
69 const std::string& interface) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040070
Brad Bishopd1eac882018-03-29 10:34:05 -040071 /** @brief Dbus signal callback for PropertiesChanged.
72 *
73 * Subclasses to update the cache.
74 *
75 * @param[in] message - The org.freedesktop.DBus.PropertiesChanged
76 * message.
77 * @param[in] path - The path associated with the message.
78 * @param[in] interface - The interface associated with the message.
79 */
80 virtual void propertiesChanged(sdbusplus::message::message&,
81 const std::string& path,
82 const std::string& interface) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040083
Brad Bishopd1eac882018-03-29 10:34:05 -040084 /** @brief Dbus signal callback for InterfacesAdded.
85 *
86 * Subclasses to update the cache.
87 *
88 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
89 * message.
90 */
91 virtual void interfacesAdded(sdbusplus::message::message& msg) = 0;
Brad Bishop4b916f12017-05-23 18:06:38 -040092
Brad Bishopd1eac882018-03-29 10:34:05 -040093 protected:
94 /** @brief Property names and their associated storage. */
95 const PropertyIndex& index;
Brad Bishop4b916f12017-05-23 18:06:38 -040096
Brad Bishopd1eac882018-03-29 10:34:05 -040097 /** @brief Optional callback method. */
98 Callback* const cb;
Brad Bishop4b916f12017-05-23 18:06:38 -040099
Brad Bishopd1eac882018-03-29 10:34:05 -0400100 /** @brief The start method should only be invoked once. */
101 bool alreadyRan;
Brad Bishop4b916f12017-05-23 18:06:38 -0400102};
103
104/** @class PropertyWatchOfType
105 * @brief Type specific logic for PropertyWatch.
106 *
107 * @tparam DBusInterfaceType - DBus access delegate.
108 * @tparam T - The type of the properties being watched.
109 */
110template <typename T, typename DBusInterfaceType>
111class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
112{
Brad Bishopd1eac882018-03-29 10:34:05 -0400113 public:
114 PropertyWatchOfType() = default;
115 PropertyWatchOfType(const PropertyWatchOfType&) = delete;
116 PropertyWatchOfType(PropertyWatchOfType&&) = default;
117 PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete;
118 PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default;
119 ~PropertyWatchOfType() = default;
Matthew Barthefe01582019-09-09 15:22:37 -0500120 PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback,
121 Filters* filterOps = nullptr) :
Matthew Barthae786ef2019-09-04 15:46:13 -0500122 PropertyWatch<DBusInterfaceType>(watchIndex, &callback),
123 filterOps(filterOps)
Brad Bishopd1eac882018-03-29 10:34:05 -0400124 {
125 }
Matthew Barthefe01582019-09-09 15:22:37 -0500126 PropertyWatchOfType(const PropertyIndex& watchIndex,
127 Filters* filterOps = nullptr) :
Matthew Barthae786ef2019-09-04 15:46:13 -0500128 PropertyWatch<DBusInterfaceType>(watchIndex, nullptr),
129 filterOps(filterOps)
Brad Bishopd1eac882018-03-29 10:34:05 -0400130 {
131 }
Brad Bishopfccdc392017-05-22 21:11:09 -0400132
Brad Bishopd1eac882018-03-29 10:34:05 -0400133 /** @brief PropertyMatch implementation for PropertyWatchOfType.
134 *
135 * @param[in] busName - The busname hosting the interface to query.
136 * @param[in] path - The path of the interface to query.
137 * @param[in] interface - The interface to query.
138 */
139 void updateProperties(const std::string& busName, const std::string& path,
140 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400141
Brad Bishopd1eac882018-03-29 10:34:05 -0400142 /** @brief PropertyMatch implementation for PropertyWatchOfType.
143 *
144 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
145 * message.
146 * @param[in] path - The path associated with the message.
147 * @param[in] interface - The interface associated with the message.
148 */
149 void propertiesChanged(sdbusplus::message::message& msg,
150 const std::string& path,
151 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400152
Brad Bishopd1eac882018-03-29 10:34:05 -0400153 /** @brief DBus agnostic implementation of interfacesAdded.
154 *
155 * @param[in] path - The path of the properties that changed.
156 * @param[in] interface - The interface of the properties that
157 * changed.
158 * @param[in] properites - The properties that changed.
159 */
160 void propertiesChanged(const std::string& path,
161 const std::string& interface,
162 const PropertiesChanged<T>& properties);
Brad Bishop4b916f12017-05-23 18:06:38 -0400163
Brad Bishopd1eac882018-03-29 10:34:05 -0400164 /** @brief PropertyMatch implementation for PropertyWatchOfType.
165 *
166 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
167 * message.
168 */
169 void interfacesAdded(sdbusplus::message::message& msg) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400170
Brad Bishopd1eac882018-03-29 10:34:05 -0400171 /** @brief DBus agnostic implementation of interfacesAdded.
172 *
173 * @param[in] path - The path of the added interfaces.
174 * @param[in] interfaces - The added interfaces.
175 */
176 void interfacesAdded(const std::string& path,
177 const InterfacesAdded<T>& interfaces);
Matthew Barthae786ef2019-09-04 15:46:13 -0500178
179 private:
Matthew Barthefe01582019-09-09 15:22:37 -0500180 /** @brief Optional filter operations to perform on property changes. */
181 Filters* const filterOps;
Brad Bishop4b916f12017-05-23 18:06:38 -0400182};
183
184} // namespace monitoring
185} // namespace dbus
186} // namespace phosphor