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