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* cb = nullptr)
41             : Watch(), index(watchIndex), callback(cb), alreadyRan(false) {}
42 
43         /** @brief Start the watch.
44          *
45          *  Watch start interface implementation for PropertyWatch.
46          */
47         void start() override;
48 
49         /** @brief Update properties.
50          *
51          *  Subclasses to query the properties specified by the index
52          *  and update the cache.
53          *
54          *  @param[in] busName - The busname hosting the interface to query.
55          *  @param[in] path - The path of the interface to query.
56          *  @param[in] interface - The interface to query.
57          */
58         virtual void updateProperties(
59             const std::string& busName,
60             const std::string& path,
61             const std::string& interface) = 0;
62 
63         /** @brief Dbus signal callback for PropertiesChanged.
64          *
65          *  Subclasses to update the cache.
66          *
67          *  @param[in] message - The org.freedesktop.DBus.PropertiesChanged
68          *               message.
69          *  @param[in] path - The path associated with the message.
70          *  @param[in] interface - The interface associated with the message.
71          */
72         virtual void propertiesChanged(
73             sdbusplus::message::message&,
74             const std::string& path,
75             const std::string& interface) = 0;
76 
77         /** @brief Dbus signal callback for InterfacesAdded.
78          *
79          *  Subclasses to update the cache.
80          *
81          *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
82          *               message.
83          */
84         virtual void interfacesAdded(sdbusplus::message::message& msg) = 0;
85 
86     protected:
87 
88         /** @brief Property names and their associated storage. */
89         const PropertyIndex& index;
90 
91         /** @brief Optional callback method. */
92         Callback* const callback;
93 
94         /** @brief The start method should only be invoked once. */
95         bool alreadyRan;
96 };
97 
98 /** @class PropertyWatchOfType
99  *  @brief Type specific logic for PropertyWatch.
100  *
101  *  @tparam DBusInterfaceType - DBus access delegate.
102  *  @tparam T - The type of the properties being watched.
103  */
104 template <typename T, typename DBusInterfaceType>
105 class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
106 {
107     public:
108         PropertyWatchOfType() = default;
109         PropertyWatchOfType(const PropertyWatchOfType&) = delete;
110         PropertyWatchOfType(PropertyWatchOfType&&) = default;
111         PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete;
112         PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default;
113         ~PropertyWatchOfType() = default;
114         PropertyWatchOfType(
115             const PropertyIndex& watchIndex, Callback& callback) :
116             PropertyWatch<DBusInterfaceType>(watchIndex, &callback) {}
117         PropertyWatchOfType(
118             const PropertyIndex& watchIndex) :
119             PropertyWatch<DBusInterfaceType>(watchIndex, nullptr) {}
120 
121 
122         /** @brief PropertyMatch implementation for PropertyWatchOfType.
123          *
124          *  @param[in] busName - The busname hosting the interface to query.
125          *  @param[in] path - The path of the interface to query.
126          *  @param[in] interface - The interface to query.
127          */
128         void updateProperties(
129             const std::string& busName,
130             const std::string& path,
131             const std::string& interface) override;
132 
133         /** @brief PropertyMatch implementation for PropertyWatchOfType.
134          *
135          *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
136          *               message.
137          *  @param[in] path - The path associated with the message.
138          *  @param[in] interface - The interface associated with the message.
139          */
140         void propertiesChanged(
141             sdbusplus::message::message& msg,
142             const std::string& path,
143             const std::string& interface) override;
144 
145         /** @brief DBus agnostic implementation of interfacesAdded.
146          *
147          *  @param[in] path - The path of the properties that changed.
148          *  @param[in] interface - The interface of the properties that
149          *                  changed.
150          *  @param[in] properites - The properties that changed.
151          */
152         void propertiesChanged(
153             const std::string& path,
154             const std::string& interface,
155             const PropertiesChanged<T>& properties);
156 
157         /** @brief PropertyMatch implementation for PropertyWatchOfType.
158          *
159          *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
160          *               message.
161          */
162         void interfacesAdded(sdbusplus::message::message& msg) override;
163 
164         /** @brief DBus agnostic implementation of interfacesAdded.
165          *
166          *  @param[in] path - The path of the added interfaces.
167          *  @param[in] interfaces - The added interfaces.
168          */
169         void interfacesAdded(
170             const std::string& path,
171             const InterfacesAdded<T>& interfaces);
172 };
173 
174 } // namespace monitoring
175 } // namespace dbus
176 } // namespace phosphor
177