Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 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" |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 11 | #include "filters.hpp" |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 12 | #include "watch.hpp" |
| 13 | |
Andrew Geissler | ae4c95c | 2020-05-16 13:58:53 -0500 | [diff] [blame] | 14 | #include <string> |
| 15 | |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 16 | namespace phosphor |
| 17 | { |
| 18 | namespace dbus |
| 19 | { |
| 20 | namespace monitoring |
| 21 | { |
| 22 | |
Brad Bishop | fccdc39 | 2017-05-22 21:11:09 -0400 | [diff] [blame] | 23 | class Callback; |
| 24 | |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 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 | */ |
Patrick Venture | 3d6d318 | 2018-08-31 09:33:09 -0700 | [diff] [blame] | 31 | template <typename DBusInterfaceType> |
| 32 | class PropertyWatch : public Watch |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 33 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 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, |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame^] | 42 | bool ignoreStartCallback = false, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 43 | Callback* callback = nullptr) : |
| 44 | Watch(), |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame^] | 45 | index(watchIndex), cb(callback), alreadyRan(false), |
| 46 | ignoreStartCallback(ignoreStartCallback) |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 47 | { |
| 48 | } |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 49 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 50 | /** @brief Start the watch. |
| 51 | * |
| 52 | * Watch start interface implementation for PropertyWatch. |
| 53 | */ |
| 54 | void start() override; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 55 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 56 | /** @brief Run the watch callback method. |
| 57 | * |
| 58 | * Watch callback interface implementation for PropertyWatch. |
| 59 | */ |
| 60 | void callback(Context ctx) override; |
Brad Bishop | ce4fbe1 | 2017-06-06 23:58:09 -0400 | [diff] [blame] | 61 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 62 | /** @brief Update properties. |
| 63 | * |
| 64 | * Subclasses to query the properties specified by the index |
| 65 | * and update the cache. |
| 66 | * |
| 67 | * @param[in] busName - The busname hosting the interface to query. |
| 68 | * @param[in] path - The path of the interface to query. |
| 69 | * @param[in] interface - The interface to query. |
| 70 | */ |
| 71 | virtual void updateProperties(const std::string& busName, |
| 72 | const std::string& path, |
| 73 | const std::string& interface) = 0; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 74 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 75 | /** @brief Dbus signal callback for PropertiesChanged. |
| 76 | * |
| 77 | * Subclasses to update the cache. |
| 78 | * |
| 79 | * @param[in] message - The org.freedesktop.DBus.PropertiesChanged |
| 80 | * message. |
| 81 | * @param[in] path - The path associated with the message. |
| 82 | * @param[in] interface - The interface associated with the message. |
| 83 | */ |
| 84 | virtual void propertiesChanged(sdbusplus::message::message&, |
| 85 | const std::string& path, |
| 86 | const std::string& interface) = 0; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 87 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 88 | /** @brief Dbus signal callback for InterfacesAdded. |
| 89 | * |
| 90 | * Subclasses to update the cache. |
| 91 | * |
| 92 | * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged |
| 93 | * message. |
| 94 | */ |
| 95 | virtual void interfacesAdded(sdbusplus::message::message& msg) = 0; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 96 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 97 | protected: |
| 98 | /** @brief Property names and their associated storage. */ |
| 99 | const PropertyIndex& index; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 100 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 101 | /** @brief Optional callback method. */ |
| 102 | Callback* const cb; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 103 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 104 | /** @brief The start method should only be invoked once. */ |
| 105 | bool alreadyRan; |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame^] | 106 | |
| 107 | /** @brief Ignore callback on start */ |
| 108 | bool ignoreStartCallback; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /** @class PropertyWatchOfType |
| 112 | * @brief Type specific logic for PropertyWatch. |
| 113 | * |
| 114 | * @tparam DBusInterfaceType - DBus access delegate. |
| 115 | * @tparam T - The type of the properties being watched. |
| 116 | */ |
| 117 | template <typename T, typename DBusInterfaceType> |
| 118 | class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType> |
| 119 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 120 | public: |
| 121 | PropertyWatchOfType() = default; |
| 122 | PropertyWatchOfType(const PropertyWatchOfType&) = delete; |
| 123 | PropertyWatchOfType(PropertyWatchOfType&&) = default; |
| 124 | PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete; |
| 125 | PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default; |
| 126 | ~PropertyWatchOfType() = default; |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 127 | PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback, |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame^] | 128 | bool ignoreStartCallback = false, |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 129 | Filters* filterOps = nullptr) : |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame^] | 130 | PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback, |
| 131 | &callback), |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 132 | filterOps(filterOps) |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 133 | { |
| 134 | } |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 135 | PropertyWatchOfType(const PropertyIndex& watchIndex, |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame^] | 136 | bool ignoreStartCallback = false, |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 137 | Filters* filterOps = nullptr) : |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame^] | 138 | PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback, |
| 139 | nullptr), |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 140 | filterOps(filterOps) |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 141 | { |
| 142 | } |
Brad Bishop | fccdc39 | 2017-05-22 21:11:09 -0400 | [diff] [blame] | 143 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 144 | /** @brief PropertyMatch implementation for PropertyWatchOfType. |
| 145 | * |
| 146 | * @param[in] busName - The busname hosting the interface to query. |
| 147 | * @param[in] path - The path of the interface to query. |
| 148 | * @param[in] interface - The interface to query. |
| 149 | */ |
| 150 | void updateProperties(const std::string& busName, const std::string& path, |
| 151 | const std::string& interface) override; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 152 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 153 | /** @brief PropertyMatch implementation for PropertyWatchOfType. |
| 154 | * |
| 155 | * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged |
| 156 | * message. |
| 157 | * @param[in] path - The path associated with the message. |
| 158 | * @param[in] interface - The interface associated with the message. |
| 159 | */ |
| 160 | void propertiesChanged(sdbusplus::message::message& msg, |
| 161 | const std::string& path, |
| 162 | const std::string& interface) override; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 163 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 164 | /** @brief DBus agnostic implementation of interfacesAdded. |
| 165 | * |
| 166 | * @param[in] path - The path of the properties that changed. |
| 167 | * @param[in] interface - The interface of the properties that |
| 168 | * changed. |
| 169 | * @param[in] properites - The properties that changed. |
| 170 | */ |
| 171 | void propertiesChanged(const std::string& path, |
| 172 | const std::string& interface, |
| 173 | const PropertiesChanged<T>& properties); |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 174 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 175 | /** @brief PropertyMatch implementation for PropertyWatchOfType. |
| 176 | * |
| 177 | * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged |
| 178 | * message. |
| 179 | */ |
| 180 | void interfacesAdded(sdbusplus::message::message& msg) override; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 181 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 182 | /** @brief DBus agnostic implementation of interfacesAdded. |
| 183 | * |
| 184 | * @param[in] path - The path of the added interfaces. |
| 185 | * @param[in] interfaces - The added interfaces. |
| 186 | */ |
| 187 | void interfacesAdded(const std::string& path, |
| 188 | const InterfacesAdded<T>& interfaces); |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 189 | |
| 190 | private: |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 191 | /** @brief Optional filter operations to perform on property changes. */ |
| 192 | Filters* const filterOps; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | } // namespace monitoring |
| 196 | } // namespace dbus |
| 197 | } // namespace phosphor |