xref: /openbmc/phosphor-dbus-monitor/src/propertywatch.hpp (revision eab4f8c0a047e1aaedf74d6144d83132d1b003de)
14b916f13SBrad Bishop /**
24b916f13SBrad Bishop  * @file propertywatch.hpp
34b916f13SBrad Bishop  * @brief PropertyWatch class declarations.
44b916f13SBrad Bishop  *
54b916f13SBrad Bishop  * In general class users should include propertywatchimpl.hpp instead to avoid
64b916f13SBrad Bishop  * link failures.
74b916f13SBrad Bishop  */
84b916f13SBrad Bishop #pragma once
94b916f13SBrad Bishop 
104b916f13SBrad Bishop #include "data_types.hpp"
11efe0158cSMatthew Barth #include "filters.hpp"
124b916f13SBrad Bishop #include "watch.hpp"
134b916f13SBrad Bishop 
14ae4c95c6SAndrew Geissler #include <string>
15ae4c95c6SAndrew Geissler 
164b916f13SBrad Bishop namespace phosphor
174b916f13SBrad Bishop {
184b916f13SBrad Bishop namespace dbus
194b916f13SBrad Bishop {
204b916f13SBrad Bishop namespace monitoring
214b916f13SBrad Bishop {
224b916f13SBrad Bishop 
23fccdc39fSBrad Bishop class Callback;
24fccdc39fSBrad Bishop 
254b916f13SBrad Bishop /** @class PropertyWatch
264b916f13SBrad Bishop  *  @brief Type agnostic, factored out logic for property watches.
274b916f13SBrad Bishop  *
284b916f13SBrad Bishop  *  A property watch maintains the state of one or more DBus properties
294b916f13SBrad Bishop  *  as specified by the supplied index.
304b916f13SBrad Bishop  */
313d6d3182SPatrick Venture template <typename DBusInterfaceType>
323d6d3182SPatrick Venture class PropertyWatch : public Watch
334b916f13SBrad Bishop {
344b916f13SBrad Bishop   public:
354b916f13SBrad Bishop     PropertyWatch() = delete;
364b916f13SBrad Bishop     PropertyWatch(const PropertyWatch&) = delete;
374b916f13SBrad Bishop     PropertyWatch(PropertyWatch&&) = default;
384b916f13SBrad Bishop     PropertyWatch& operator=(const PropertyWatch&) = delete;
394b916f13SBrad Bishop     PropertyWatch& operator=(PropertyWatch&&) = default;
404b916f13SBrad Bishop     virtual ~PropertyWatch() = default;
PropertyWatch(const PropertyIndex & watchIndex,bool ignoreStartCallback=false,Callback * callback=nullptr)41d1eac88dSBrad Bishop     PropertyWatch(const PropertyIndex& watchIndex,
4298d6462aSLei YU                   bool ignoreStartCallback = false,
43d1eac88dSBrad Bishop                   Callback* callback = nullptr) :
44*eab4f8c0SPatrick Williams         Watch(), index(watchIndex), cb(callback), alreadyRan(false),
4598d6462aSLei YU         ignoreStartCallback(ignoreStartCallback)
463fe976ccSGeorge Liu     {}
474b916f13SBrad Bishop 
484b916f13SBrad Bishop     /** @brief Start the watch.
494b916f13SBrad Bishop      *
504b916f13SBrad Bishop      *  Watch start interface implementation for PropertyWatch.
514b916f13SBrad Bishop      */
524b916f13SBrad Bishop     void start() override;
534b916f13SBrad Bishop 
54ce4fbe11SBrad Bishop     /** @brief Run the watch callback method.
55ce4fbe11SBrad Bishop      *
56ce4fbe11SBrad Bishop      *  Watch callback interface implementation for PropertyWatch.
57ce4fbe11SBrad Bishop      */
58a45e086dSRatan Gupta     void callback(Context ctx) override;
59ce4fbe11SBrad Bishop 
604b916f13SBrad Bishop     /** @brief Update properties.
614b916f13SBrad Bishop      *
624b916f13SBrad Bishop      *  Subclasses to query the properties specified by the index
634b916f13SBrad Bishop      *  and update the cache.
644b916f13SBrad Bishop      *
654b916f13SBrad Bishop      *  @param[in] busName - The busname hosting the interface to query.
664b916f13SBrad Bishop      *  @param[in] path - The path of the interface to query.
674b916f13SBrad Bishop      *  @param[in] interface - The interface to query.
684b916f13SBrad Bishop      */
69d1eac88dSBrad Bishop     virtual void updateProperties(const std::string& busName,
704b916f13SBrad Bishop                                   const std::string& path,
714b916f13SBrad Bishop                                   const std::string& interface) = 0;
724b916f13SBrad Bishop 
734b916f13SBrad Bishop     /** @brief Dbus signal callback for PropertiesChanged.
744b916f13SBrad Bishop      *
754b916f13SBrad Bishop      *  Subclasses to update the cache.
764b916f13SBrad Bishop      *
774b916f13SBrad Bishop      *  @param[in] message - The org.freedesktop.DBus.PropertiesChanged
784b916f13SBrad Bishop      *               message.
794b916f13SBrad Bishop      *  @param[in] path - The path associated with the message.
804b916f13SBrad Bishop      *  @param[in] interface - The interface associated with the message.
814b916f13SBrad Bishop      */
82413a4857SPatrick Williams     virtual void propertiesChanged(sdbusplus::message_t&,
834b916f13SBrad Bishop                                    const std::string& path,
844b916f13SBrad Bishop                                    const std::string& interface) = 0;
854b916f13SBrad Bishop 
864b916f13SBrad Bishop     /** @brief Dbus signal callback for InterfacesAdded.
874b916f13SBrad Bishop      *
884b916f13SBrad Bishop      *  Subclasses to update the cache.
894b916f13SBrad Bishop      *
904b916f13SBrad Bishop      *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
914b916f13SBrad Bishop      *               message.
924b916f13SBrad Bishop      */
93413a4857SPatrick Williams     virtual void interfacesAdded(sdbusplus::message_t& msg) = 0;
944b916f13SBrad Bishop 
954b916f13SBrad Bishop   protected:
964b916f13SBrad Bishop     /** @brief Property names and their associated storage. */
974b916f13SBrad Bishop     const PropertyIndex& index;
984b916f13SBrad Bishop 
99fccdc39fSBrad Bishop     /** @brief Optional callback method. */
100ce4fbe11SBrad Bishop     Callback* const cb;
1014b916f13SBrad Bishop 
1024b916f13SBrad Bishop     /** @brief The start method should only be invoked once. */
1034b916f13SBrad Bishop     bool alreadyRan;
10498d6462aSLei YU 
10598d6462aSLei YU     /** @brief Ignore callback on start */
10698d6462aSLei YU     bool ignoreStartCallback;
1074b916f13SBrad Bishop };
1084b916f13SBrad Bishop 
1094b916f13SBrad Bishop /** @class PropertyWatchOfType
1104b916f13SBrad Bishop  *  @brief Type specific logic for PropertyWatch.
1114b916f13SBrad Bishop  *
1124b916f13SBrad Bishop  *  @tparam DBusInterfaceType - DBus access delegate.
1134b916f13SBrad Bishop  *  @tparam T - The type of the properties being watched.
1144b916f13SBrad Bishop  */
1154b916f13SBrad Bishop template <typename T, typename DBusInterfaceType>
1164b916f13SBrad Bishop class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType>
1174b916f13SBrad Bishop {
1184b916f13SBrad Bishop   public:
1194b916f13SBrad Bishop     PropertyWatchOfType() = default;
1204b916f13SBrad Bishop     PropertyWatchOfType(const PropertyWatchOfType&) = delete;
1214b916f13SBrad Bishop     PropertyWatchOfType(PropertyWatchOfType&&) = default;
1224b916f13SBrad Bishop     PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete;
1234b916f13SBrad Bishop     PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default;
1244b916f13SBrad Bishop     ~PropertyWatchOfType() = default;
PropertyWatchOfType(const PropertyIndex & watchIndex,Callback & callback,bool ignoreStartCallback=false,Filters * filterOps=nullptr)125efe0158cSMatthew Barth     PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback,
12698d6462aSLei YU                         bool ignoreStartCallback = false,
127efe0158cSMatthew Barth                         Filters* filterOps = nullptr) :
12898d6462aSLei YU         PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback,
12998d6462aSLei YU                                          &callback),
130ae786ef6SMatthew Barth         filterOps(filterOps)
1313fe976ccSGeorge Liu     {}
PropertyWatchOfType(const PropertyIndex & watchIndex,bool ignoreStartCallback=false,Filters * filterOps=nullptr)132ecef1191SGeorge Liu     explicit PropertyWatchOfType(const PropertyIndex& watchIndex,
13398d6462aSLei YU                                  bool ignoreStartCallback = false,
134efe0158cSMatthew Barth                                  Filters* filterOps = nullptr) :
13598d6462aSLei YU         PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback,
13698d6462aSLei YU                                          nullptr),
137ae786ef6SMatthew Barth         filterOps(filterOps)
1383fe976ccSGeorge Liu     {}
1394b916f13SBrad Bishop 
1404b916f13SBrad Bishop     /** @brief PropertyMatch implementation for PropertyWatchOfType.
1414b916f13SBrad Bishop      *
1424b916f13SBrad Bishop      *  @param[in] busName - The busname hosting the interface to query.
1434b916f13SBrad Bishop      *  @param[in] path - The path of the interface to query.
1444b916f13SBrad Bishop      *  @param[in] interface - The interface to query.
1454b916f13SBrad Bishop      */
146d1eac88dSBrad Bishop     void updateProperties(const std::string& busName, const std::string& path,
1474b916f13SBrad Bishop                           const std::string& interface) override;
1484b916f13SBrad Bishop 
1494b916f13SBrad Bishop     /** @brief PropertyMatch implementation for PropertyWatchOfType.
1504b916f13SBrad Bishop      *
1514b916f13SBrad Bishop      *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
1524b916f13SBrad Bishop      *               message.
1534b916f13SBrad Bishop      *  @param[in] path - The path associated with the message.
1544b916f13SBrad Bishop      *  @param[in] interface - The interface associated with the message.
1554b916f13SBrad Bishop      */
156413a4857SPatrick Williams     void propertiesChanged(sdbusplus::message_t& msg, const std::string& path,
1574b916f13SBrad Bishop                            const std::string& interface) override;
1584b916f13SBrad Bishop 
1594b916f13SBrad Bishop     /** @brief DBus agnostic implementation of interfacesAdded.
1604b916f13SBrad Bishop      *
1614b916f13SBrad Bishop      *  @param[in] path - The path of the properties that changed.
1624b916f13SBrad Bishop      *  @param[in] interface - The interface of the properties that
1634b916f13SBrad Bishop      *                  changed.
1640c1e024fSManojkiran Eda      *  @param[in] properties - The properties that changed.
1654b916f13SBrad Bishop      */
166d1eac88dSBrad Bishop     void propertiesChanged(const std::string& path,
1674b916f13SBrad Bishop                            const std::string& interface,
1684b916f13SBrad Bishop                            const PropertiesChanged<T>& properties);
1694b916f13SBrad Bishop 
1704b916f13SBrad Bishop     /** @brief PropertyMatch implementation for PropertyWatchOfType.
1714b916f13SBrad Bishop      *
1724b916f13SBrad Bishop      *  @param[in] msg - The org.freedesktop.DBus.PropertiesChanged
1734b916f13SBrad Bishop      *               message.
1744b916f13SBrad Bishop      */
175413a4857SPatrick Williams     void interfacesAdded(sdbusplus::message_t& msg) override;
1764b916f13SBrad Bishop 
1774b916f13SBrad Bishop     /** @brief DBus agnostic implementation of interfacesAdded.
1784b916f13SBrad Bishop      *
1794b916f13SBrad Bishop      *  @param[in] path - The path of the added interfaces.
1804b916f13SBrad Bishop      *  @param[in] interfaces - The added interfaces.
1814b916f13SBrad Bishop      */
182d1eac88dSBrad Bishop     void interfacesAdded(const std::string& path,
1834b916f13SBrad Bishop                          const InterfacesAdded<T>& interfaces);
184ae786ef6SMatthew Barth 
185ae786ef6SMatthew Barth   private:
186efe0158cSMatthew Barth     /** @brief Optional filter operations to perform on property changes. */
187efe0158cSMatthew Barth     Filters* const filterOps;
1884b916f13SBrad Bishop };
1894b916f13SBrad Bishop 
1904b916f13SBrad Bishop } // namespace monitoring
1914b916f13SBrad Bishop } // namespace dbus
1924b916f13SBrad Bishop } // namespace phosphor
193