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