blob: a94b99ea0705dacc0f3953f298a89974e5415e72 [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;
119 PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback) :
120 PropertyWatch<DBusInterfaceType>(watchIndex, &callback)
121 {
122 }
123 PropertyWatchOfType(const PropertyIndex& watchIndex) :
124 PropertyWatch<DBusInterfaceType>(watchIndex, nullptr)
125 {
126 }
Brad Bishopfccdc392017-05-22 21:11:09 -0400127
Brad Bishopd1eac882018-03-29 10:34:05 -0400128 /** @brief PropertyMatch implementation for PropertyWatchOfType.
129 *
130 * @param[in] busName - The busname hosting the interface to query.
131 * @param[in] path - The path of the interface to query.
132 * @param[in] interface - The interface to query.
133 */
134 void updateProperties(const std::string& busName, const std::string& path,
135 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400136
Brad Bishopd1eac882018-03-29 10:34:05 -0400137 /** @brief PropertyMatch implementation for PropertyWatchOfType.
138 *
139 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
140 * message.
141 * @param[in] path - The path associated with the message.
142 * @param[in] interface - The interface associated with the message.
143 */
144 void propertiesChanged(sdbusplus::message::message& msg,
145 const std::string& path,
146 const std::string& interface) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400147
Brad Bishopd1eac882018-03-29 10:34:05 -0400148 /** @brief DBus agnostic implementation of interfacesAdded.
149 *
150 * @param[in] path - The path of the properties that changed.
151 * @param[in] interface - The interface of the properties that
152 * changed.
153 * @param[in] properites - The properties that changed.
154 */
155 void propertiesChanged(const std::string& path,
156 const std::string& interface,
157 const PropertiesChanged<T>& properties);
Brad Bishop4b916f12017-05-23 18:06:38 -0400158
Brad Bishopd1eac882018-03-29 10:34:05 -0400159 /** @brief PropertyMatch implementation for PropertyWatchOfType.
160 *
161 * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
162 * message.
163 */
164 void interfacesAdded(sdbusplus::message::message& msg) override;
Brad Bishop4b916f12017-05-23 18:06:38 -0400165
Brad Bishopd1eac882018-03-29 10:34:05 -0400166 /** @brief DBus agnostic implementation of interfacesAdded.
167 *
168 * @param[in] path - The path of the added interfaces.
169 * @param[in] interfaces - The added interfaces.
170 */
171 void interfacesAdded(const std::string& path,
172 const InterfacesAdded<T>& interfaces);
Brad Bishop4b916f12017-05-23 18:06:38 -0400173};
174
175} // namespace monitoring
176} // namespace dbus
177} // namespace phosphor