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