1 /**
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 
13 namespace phosphor
14 {
15 namespace dbus
16 {
17 namespace monitoring
18 {
19 
20 class Callback;
21 
22 /** @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  */
28 template <typename DBusInterfaceType>
29 class PropertyWatch : public Watch
30 {
31     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(
39             const PropertyIndex& watchIndex,
40             Callback* callback = nullptr)
41             : Watch(), index(watchIndex), cb(callback), alreadyRan(false) {}
42 
43         /** @brief Start the watch.
44          *
45          *  Watch start interface implementation for PropertyWatch.
46          */
47         void start() override;
48 
49         /** @brief Run the watch callback method.
50          *
51          *  Watch callback interface implementation for PropertyWatch.
52          */
53         void callback() override;
54 
55         /** @brief Update properties.
56          *
57          *  Subclasses to query the properties specified by the index
58          *  and update the cache.
59          *
60          *  @param[in] busName - The busname hosting the interface to query.
61          *  @param[in] path - The path of the interface to query.
62          *  @param[in] interface - The interface to query.
63          */
64         virtual void updateProperties(
65             const std::string& busName,
66             const std::string& path,
67             const std::string& interface) = 0;
68 
69         /** @brief Dbus signal callback for PropertiesChanged.
70          *
71          *  Subclasses to update the cache.
72          *
73          *  @param[in] message - The org.freedesktop.DBus.PropertiesChanged
74          *               message.
75          *  @param[in] path - The path associated with the message.
76          *  @param[in] interface - The interface associated with the message.
77          */
78         virtual void propertiesChanged(
79             sdbusplus::message::message&,
80             const std::string& path,
81             const std::string& interface) = 0;
82 
83         /** @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;
91 
92     protected:
93 
94         /** @brief Property names and their associated storage. */
95         const PropertyIndex& index;
96 
97         /** @brief Optional callback method. */
98         Callback* const cb;
99 
100         /** @brief The start method should only be invoked once. */
101         bool alreadyRan;
102 };
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  */
110 template <typename T, typename DBusInterfaceType>
111 class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
112 {
113     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;
120         PropertyWatchOfType(
121             const PropertyIndex& watchIndex, Callback& callback) :
122             PropertyWatch<DBusInterfaceType>(watchIndex, &callback) {}
123         PropertyWatchOfType(
124             const PropertyIndex& watchIndex) :
125             PropertyWatch<DBusInterfaceType>(watchIndex, nullptr) {}
126 
127 
128         /** @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(
135             const std::string& busName,
136             const std::string& path,
137             const std::string& interface) override;
138 
139         /** @brief PropertyMatch implementation for PropertyWatchOfType.
140          *
141          *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
142          *               message.
143          *  @param[in] path - The path associated with the message.
144          *  @param[in] interface - The interface associated with the message.
145          */
146         void propertiesChanged(
147             sdbusplus::message::message& msg,
148             const std::string& path,
149             const std::string& interface) override;
150 
151         /** @brief DBus agnostic implementation of interfacesAdded.
152          *
153          *  @param[in] path - The path of the properties that changed.
154          *  @param[in] interface - The interface of the properties that
155          *                  changed.
156          *  @param[in] properites - The properties that changed.
157          */
158         void propertiesChanged(
159             const std::string& path,
160             const std::string& interface,
161             const PropertiesChanged<T>& properties);
162 
163         /** @brief PropertyMatch implementation for PropertyWatchOfType.
164          *
165          *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
166          *               message.
167          */
168         void interfacesAdded(sdbusplus::message::message& msg) override;
169 
170         /** @brief DBus agnostic implementation of interfacesAdded.
171          *
172          *  @param[in] path - The path of the added interfaces.
173          *  @param[in] interfaces - The added interfaces.
174          */
175         void interfacesAdded(
176             const std::string& path,
177             const InterfacesAdded<T>& interfaces);
178 };
179 
180 } // namespace monitoring
181 } // namespace dbus
182 } // namespace phosphor
183