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) : |
Patrick Williams | eab4f8c | 2024-08-16 15:20:10 -0400 | [diff] [blame^] | 44 | Watch(), index(watchIndex), cb(callback), alreadyRan(false), |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame] | 45 | ignoreStartCallback(ignoreStartCallback) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 46 | {} |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 47 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 48 | /** @brief Start the watch. |
| 49 | * |
| 50 | * Watch start interface implementation for PropertyWatch. |
| 51 | */ |
| 52 | void start() override; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 53 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 54 | /** @brief Run the watch callback method. |
| 55 | * |
| 56 | * Watch callback interface implementation for PropertyWatch. |
| 57 | */ |
| 58 | void callback(Context ctx) override; |
Brad Bishop | ce4fbe1 | 2017-06-06 23:58:09 -0400 | [diff] [blame] | 59 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 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; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 72 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 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 | */ |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 82 | virtual void propertiesChanged(sdbusplus::message_t&, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 83 | const std::string& path, |
| 84 | const std::string& interface) = 0; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 85 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 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 | */ |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 93 | virtual void interfacesAdded(sdbusplus::message_t& msg) = 0; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 94 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 95 | protected: |
| 96 | /** @brief Property names and their associated storage. */ |
| 97 | const PropertyIndex& index; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 98 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 99 | /** @brief Optional callback method. */ |
| 100 | Callback* const cb; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 101 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 102 | /** @brief The start method should only be invoked once. */ |
| 103 | bool alreadyRan; |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame] | 104 | |
| 105 | /** @brief Ignore callback on start */ |
| 106 | bool ignoreStartCallback; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /** @class PropertyWatchOfType |
| 110 | * @brief Type specific logic for PropertyWatch. |
| 111 | * |
| 112 | * @tparam DBusInterfaceType - DBus access delegate. |
| 113 | * @tparam T - The type of the properties being watched. |
| 114 | */ |
| 115 | template <typename T, typename DBusInterfaceType> |
| 116 | class PropertyWatchOfType : public PropertyWatch<DBusInterfaceType> |
| 117 | { |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 118 | public: |
| 119 | PropertyWatchOfType() = default; |
| 120 | PropertyWatchOfType(const PropertyWatchOfType&) = delete; |
| 121 | PropertyWatchOfType(PropertyWatchOfType&&) = default; |
| 122 | PropertyWatchOfType& operator=(const PropertyWatchOfType&) = delete; |
| 123 | PropertyWatchOfType& operator=(PropertyWatchOfType&&) = default; |
| 124 | ~PropertyWatchOfType() = default; |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 125 | PropertyWatchOfType(const PropertyIndex& watchIndex, Callback& callback, |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame] | 126 | bool ignoreStartCallback = false, |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 127 | Filters* filterOps = nullptr) : |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame] | 128 | PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback, |
| 129 | &callback), |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 130 | filterOps(filterOps) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 131 | {} |
George Liu | ecef119 | 2022-07-07 08:59:12 +0800 | [diff] [blame] | 132 | explicit PropertyWatchOfType(const PropertyIndex& watchIndex, |
| 133 | bool ignoreStartCallback = false, |
| 134 | Filters* filterOps = nullptr) : |
Lei YU | 98d6462 | 2022-05-24 19:11:23 +0800 | [diff] [blame] | 135 | PropertyWatch<DBusInterfaceType>(watchIndex, ignoreStartCallback, |
| 136 | nullptr), |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 137 | filterOps(filterOps) |
George Liu | 3fe976c | 2022-06-21 09:37:04 +0800 | [diff] [blame] | 138 | {} |
Brad Bishop | fccdc39 | 2017-05-22 21:11:09 -0400 | [diff] [blame] | 139 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 140 | /** @brief PropertyMatch implementation for PropertyWatchOfType. |
| 141 | * |
| 142 | * @param[in] busName - The busname hosting the interface to query. |
| 143 | * @param[in] path - The path of the interface to query. |
| 144 | * @param[in] interface - The interface to query. |
| 145 | */ |
| 146 | void updateProperties(const std::string& busName, const std::string& path, |
| 147 | const std::string& interface) override; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 148 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 149 | /** @brief PropertyMatch implementation for PropertyWatchOfType. |
| 150 | * |
| 151 | * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged |
| 152 | * message. |
| 153 | * @param[in] path - The path associated with the message. |
| 154 | * @param[in] interface - The interface associated with the message. |
| 155 | */ |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 156 | void propertiesChanged(sdbusplus::message_t& msg, const std::string& path, |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 157 | const std::string& interface) override; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 158 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 159 | /** @brief DBus agnostic implementation of interfacesAdded. |
| 160 | * |
| 161 | * @param[in] path - The path of the properties that changed. |
| 162 | * @param[in] interface - The interface of the properties that |
| 163 | * changed. |
Manojkiran Eda | 0c1e024 | 2024-06-17 12:02:59 +0530 | [diff] [blame] | 164 | * @param[in] properties - The properties that changed. |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 165 | */ |
| 166 | void propertiesChanged(const std::string& path, |
| 167 | const std::string& interface, |
| 168 | const PropertiesChanged<T>& properties); |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 169 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 170 | /** @brief PropertyMatch implementation for PropertyWatchOfType. |
| 171 | * |
| 172 | * @param[in] msg - The org.freedesktop.DBus.PropertiesChanged |
| 173 | * message. |
| 174 | */ |
Patrick Williams | 413a485 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 175 | void interfacesAdded(sdbusplus::message_t& msg) override; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 176 | |
Brad Bishop | d1eac88 | 2018-03-29 10:34:05 -0400 | [diff] [blame] | 177 | /** @brief DBus agnostic implementation of interfacesAdded. |
| 178 | * |
| 179 | * @param[in] path - The path of the added interfaces. |
| 180 | * @param[in] interfaces - The added interfaces. |
| 181 | */ |
| 182 | void interfacesAdded(const std::string& path, |
| 183 | const InterfacesAdded<T>& interfaces); |
Matthew Barth | ae786ef | 2019-09-04 15:46:13 -0500 | [diff] [blame] | 184 | |
| 185 | private: |
Matthew Barth | efe0158 | 2019-09-09 15:22:37 -0500 | [diff] [blame] | 186 | /** @brief Optional filter operations to perform on property changes. */ |
| 187 | Filters* const filterOps; |
Brad Bishop | 4b916f1 | 2017-05-23 18:06:38 -0400 | [diff] [blame] | 188 | }; |
| 189 | |
| 190 | } // namespace monitoring |
| 191 | } // namespace dbus |
| 192 | } // namespace phosphor |