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 "filters.hpp"
12 #include "watch.hpp"
13 
14 #include <string>
15 
16 namespace phosphor
17 {
18 namespace dbus
19 {
20 namespace monitoring
21 {
22 
23 class Callback;
24 
25 /** @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  */
31 template <typename DBusInterfaceType>
32 class PropertyWatch : public Watch
33 {
34   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     }
47 
48     /** @brief Start the watch.
49      *
50      *  Watch start interface implementation for PropertyWatch.
51      */
52     void start() override;
53 
54     /** @brief Run the watch callback method.
55      *
56      *  Watch callback interface implementation for PropertyWatch.
57      */
58     void callback(Context ctx) override;
59 
60     /** @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;
72 
73     /** @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;
85 
86     /** @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;
94 
95   protected:
96     /** @brief Property names and their associated storage. */
97     const PropertyIndex& index;
98 
99     /** @brief Optional callback method. */
100     Callback* const cb;
101 
102     /** @brief The start method should only be invoked once. */
103     bool alreadyRan;
104 };
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  */
112 template <typename T, typename DBusInterfaceType>
113 class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
114 {
115   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;
122     PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback,
123                         Filters* filterOps = nullptr) :
124         PropertyWatch<DBusInterfaceType>(watchIndex, &callback),
125         filterOps(filterOps)
126     {
127     }
128     PropertyWatchOfType(const PropertyIndex& watchIndex,
129                         Filters* filterOps = nullptr) :
130         PropertyWatch<DBusInterfaceType>(watchIndex, nullptr),
131         filterOps(filterOps)
132     {
133     }
134 
135     /** @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;
143 
144     /** @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;
154 
155     /** @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);
165 
166     /** @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;
172 
173     /** @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);
180 
181   private:
182     /** @brief Optional filter operations to perform on property changes. */
183     Filters* const filterOps;
184 };
185 
186 } // namespace monitoring
187 } // namespace dbus
188 } // namespace phosphor
189