Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "types.hpp" |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 4 | #include "sdbusplus.hpp" |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace fan |
| 10 | { |
| 11 | namespace control |
| 12 | { |
| 13 | class Zone; |
| 14 | |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 15 | using namespace phosphor::fan; |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 16 | using namespace sdbusplus::bus::match; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 17 | using namespace phosphor::logging; |
| 18 | |
| 19 | /** |
Matthew Barth | 1b3e960 | 2019-02-13 11:37:03 -0600 | [diff] [blame] | 20 | * @brief Create a zone handler function object |
| 21 | * |
| 22 | * @param[in] handler - The handler being created |
| 23 | * |
| 24 | * @return - The created zone handler function object |
| 25 | */ |
| 26 | template <typename T> |
| 27 | auto make_zoneHandler(T&& handler) |
| 28 | { |
| 29 | return ZoneHandler(std::forward<T>(handler)); |
| 30 | } |
| 31 | |
| 32 | /** |
Matthew Barth | 1b4de26 | 2018-03-06 13:03:16 -0600 | [diff] [blame] | 33 | * @brief Create a trigger function object |
| 34 | * |
| 35 | * @param[in] trigger - The trigger being created |
| 36 | * |
| 37 | * @return - The created trigger function object |
| 38 | */ |
| 39 | template <typename T> |
| 40 | auto make_trigger(T&& trigger) |
| 41 | { |
| 42 | return Trigger(std::forward<T>(trigger)); |
| 43 | } |
| 44 | |
| 45 | /** |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 46 | * @brief Create a handler function object |
| 47 | * |
| 48 | * @param[in] handler - The handler being created |
| 49 | * |
| 50 | * @return - The created handler function object |
| 51 | */ |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 52 | template <typename T, typename U> |
| 53 | auto make_handler(U&& handler) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 54 | { |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 55 | return T(std::forward<U>(handler)); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 59 | * @brief Create an action function object |
| 60 | * |
| 61 | * @param[in] action - The action being created |
| 62 | * |
| 63 | * @return - The created action function object |
| 64 | */ |
| 65 | template <typename T> |
| 66 | auto make_action(T&& action) |
| 67 | { |
| 68 | return Action(std::forward<T>(action)); |
| 69 | } |
| 70 | |
| 71 | /** |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 72 | * @struct Properties |
| 73 | * @brief A set of match filter functors for Dbus property values. Each |
| 74 | * functor provides an associated process for retrieving the value |
| 75 | * for a given property and providing it to the given handler function. |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 76 | * |
| 77 | * @tparam T - The type of the property value |
| 78 | * @tparam U - The type of the handler |
| 79 | */ |
| 80 | template <typename T, typename U> |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 81 | struct Properties |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 82 | { |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 83 | Properties() = delete; |
| 84 | ~Properties() = default; |
| 85 | Properties(const Properties&) = default; |
| 86 | Properties& operator=(const Properties&) = default; |
| 87 | Properties(Properties&&) = default; |
| 88 | Properties& operator=(Properties&&) = default; |
| 89 | explicit Properties(U&& handler) : |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 90 | _handler(std::forward<U>(handler)) { } |
| 91 | Properties(const char* path, |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 92 | const char* intf, |
| 93 | const char* prop, |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 94 | U&& handler) : |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 95 | _path(path), |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 96 | _intf(intf), |
| 97 | _prop(prop), |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 98 | _handler(std::forward<U>(handler)) { } |
| 99 | |
| 100 | /** @brief Run signal handler function |
| 101 | * |
| 102 | * Extract the property from the PropertiesChanged |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 103 | * message and run the handler function. |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 104 | */ |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 105 | void operator()(sdbusplus::bus::bus& bus, |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 106 | sdbusplus::message::message& msg, |
| 107 | Zone& zone) const |
| 108 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 109 | if (msg) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 110 | { |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 111 | std::string intf; |
| 112 | std::map<std::string, sdbusplus::message::variant<T>> props; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 113 | |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 114 | msg.read(intf); |
| 115 | if (intf != _intf) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 116 | { |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 117 | // Interface name does not match on object |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 121 | msg.read(props); |
| 122 | auto it = props.find(_prop); |
| 123 | if (it == props.cend()) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 124 | { |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 125 | // Property not included in dictionary of properties changed |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 129 | _handler(zone, _path, _intf, _prop, std::forward<T>( |
| 130 | sdbusplus::message::variant_ns::get<T>(it->second))); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 131 | } |
| 132 | else |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 133 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 134 | try |
| 135 | { |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 136 | auto val = zone.getPropertyByName<T>(_path, _intf, _prop); |
| 137 | _handler(zone, _path, _intf, _prop, std::forward<T>(val)); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 138 | } |
Matthew Barth | 86be476 | 2018-07-17 10:51:36 -0500 | [diff] [blame] | 139 | catch (const sdbusplus::exception::SdBusError&) |
| 140 | { |
| 141 | // Property will not be used unless a property changed |
| 142 | // signal message is received for this property. |
| 143 | } |
| 144 | catch (const util::DBusError&) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 145 | { |
| 146 | // Property will not be used unless a property changed |
| 147 | // signal message is received for this property. |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 148 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 149 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 150 | } |
| 151 | |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 152 | /** @brief Run init handler function |
| 153 | * |
| 154 | * Get the property from each member object of the group |
| 155 | * and run the handler function. |
| 156 | */ |
| 157 | void operator()(Zone& zone, const Group& group) const |
| 158 | { |
| 159 | std::for_each( |
| 160 | group.begin(), |
| 161 | group.end(), |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 162 | [&zone, handler = std::move(_handler)](auto const& member) |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 163 | { |
| 164 | auto path = std::get<pathPos>(member); |
| 165 | auto intf = std::get<intfPos>(member); |
| 166 | auto prop = std::get<propPos>(member); |
| 167 | try |
| 168 | { |
| 169 | auto val = zone.getPropertyByName<T>(path, intf, prop); |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 170 | handler(zone, path, intf, prop, std::forward<T>(val)); |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 171 | } |
| 172 | catch (const sdbusplus::exception::SdBusError&) |
| 173 | { |
| 174 | // Property value not sent to handler |
| 175 | } |
| 176 | catch (const util::DBusError&) |
| 177 | { |
| 178 | // Property value not sent to handler |
| 179 | } |
| 180 | } |
| 181 | ); |
| 182 | } |
| 183 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 184 | private: |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 185 | const char* _path; |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 186 | const char* _intf; |
| 187 | const char* _prop; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 188 | U _handler; |
| 189 | }; |
| 190 | |
| 191 | /** |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 192 | * @brief Used to process a Dbus properties changed signal event |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 193 | * |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 194 | * @param[in] path - Object path |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 195 | * @param[in] intf - Object interface |
| 196 | * @param[in] prop - Object property |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 197 | * @param[in] handler - Handler function to perform |
| 198 | * |
| 199 | * @tparam T - The type of the property |
| 200 | * @tparam U - The type of the handler |
| 201 | */ |
| 202 | template <typename T, typename U> |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 203 | auto propertiesChanged(const char* path, |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 204 | const char* intf, |
| 205 | const char* prop, |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 206 | U&& handler) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 207 | { |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 208 | return Properties<T, U>(path, |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 209 | intf, |
| 210 | prop, |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 211 | std::forward<U>(handler)); |
| 212 | } |
| 213 | |
| 214 | /** |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 215 | * @brief Used to get the properties of an event's group |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 216 | * |
| 217 | * @param[in] handler - Handler function to perform |
| 218 | * |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 219 | * @tparam T - The type of all the properties |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 220 | * @tparam U - The type of the handler |
| 221 | */ |
| 222 | template <typename T, typename U> |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 223 | auto getProperties(U&& handler) |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 224 | { |
| 225 | return Properties<T, U>(std::forward<U>(handler)); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 228 | /** |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 229 | * @struct Interfaces Added |
| 230 | * @brief A match filter functor for Dbus interfaces added signals |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 231 | * |
| 232 | * @tparam T - The type of the property value |
| 233 | * @tparam U - The type of the handler |
| 234 | */ |
| 235 | template <typename T, typename U> |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 236 | struct InterfacesAdded |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 237 | { |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 238 | InterfacesAdded() = delete; |
| 239 | ~InterfacesAdded() = default; |
| 240 | InterfacesAdded(const InterfacesAdded&) = default; |
| 241 | InterfacesAdded& operator=(const InterfacesAdded&) = default; |
| 242 | InterfacesAdded(InterfacesAdded&&) = default; |
| 243 | InterfacesAdded& operator=(InterfacesAdded&&) = default; |
| 244 | InterfacesAdded(const char* path, |
| 245 | const char* intf, |
| 246 | const char* prop, |
| 247 | U&& handler) : |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 248 | _path(path), |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 249 | _intf(intf), |
| 250 | _prop(prop), |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 251 | _handler(std::forward<U>(handler)) { } |
| 252 | |
| 253 | /** @brief Run signal handler function |
| 254 | * |
| 255 | * Extract the property from the InterfacesAdded |
| 256 | * message and run the handler function. |
| 257 | */ |
| 258 | void operator()(sdbusplus::bus::bus&, |
| 259 | sdbusplus::message::message& msg, |
| 260 | Zone& zone) const |
| 261 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 262 | if (msg) |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 263 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 264 | std::map<std::string, |
| 265 | std::map<std::string, |
| 266 | sdbusplus::message::variant<T>>> intfProp; |
| 267 | sdbusplus::message::object_path op; |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 268 | |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 269 | msg.read(op); |
Matthew Barth | d5cfdbe | 2017-11-14 15:29:50 -0600 | [diff] [blame] | 270 | if (static_cast<const std::string&>(op) != _path) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 271 | { |
| 272 | // Object path does not match this handler's path |
| 273 | return; |
| 274 | } |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 275 | |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 276 | msg.read(intfProp); |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 277 | auto itIntf = intfProp.find(_intf); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 278 | if (itIntf == intfProp.cend()) |
| 279 | { |
| 280 | // Interface not found on this handler's path |
| 281 | return; |
| 282 | } |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 283 | auto itProp = itIntf->second.find(_prop); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 284 | if (itProp == itIntf->second.cend()) |
| 285 | { |
| 286 | // Property not found on this handler's path |
| 287 | return; |
| 288 | } |
| 289 | |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 290 | _handler(zone, _path, _intf, _prop, std::forward<T>( |
| 291 | sdbusplus::message::variant_ns::get<T>(itProp->second))); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 292 | } |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | private: |
| 296 | const char* _path; |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 297 | const char* _intf; |
| 298 | const char* _prop; |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 299 | U _handler; |
| 300 | }; |
| 301 | |
| 302 | /** |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 303 | * @brief Used to process a Dbus interfaces added signal event |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 304 | * |
| 305 | * @param[in] path - Object path |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 306 | * @param[in] intf - Object interface |
| 307 | * @param[in] prop - Object property |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 308 | * @param[in] handler - Handler function to perform |
| 309 | * |
| 310 | * @tparam T - The type of the property |
| 311 | * @tparam U - The type of the handler |
| 312 | */ |
| 313 | template <typename T, typename U> |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 314 | auto interfacesAdded(const char* path, |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 315 | const char* intf, |
| 316 | const char* prop, |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 317 | U&& handler) |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 318 | { |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 319 | return InterfacesAdded<T, U>(path, |
| 320 | intf, |
| 321 | prop, |
| 322 | std::forward<U>(handler)); |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 325 | /** |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 326 | * @struct Interfaces Removed |
| 327 | * @brief A match filter functor for Dbus interfaces removed signals |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 328 | * |
| 329 | * @tparam U - The type of the handler |
| 330 | */ |
| 331 | template <typename U> |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 332 | struct InterfacesRemoved |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 333 | { |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 334 | InterfacesRemoved() = delete; |
| 335 | ~InterfacesRemoved() = default; |
| 336 | InterfacesRemoved(const InterfacesRemoved&) = default; |
| 337 | InterfacesRemoved& operator=(const InterfacesRemoved&) = default; |
| 338 | InterfacesRemoved(InterfacesRemoved&&) = default; |
| 339 | InterfacesRemoved& operator=(InterfacesRemoved&&) = default; |
| 340 | InterfacesRemoved(const char* path, |
| 341 | const char* intf, |
| 342 | U&& handler) : |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 343 | _path(path), |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 344 | _intf(intf), |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 345 | _handler(std::forward<U>(handler)) { } |
| 346 | |
| 347 | /** @brief Run signal handler function |
| 348 | * |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 349 | * Extract the interfaces from the InterfacesRemoved |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 350 | * message and run the handler function. |
| 351 | */ |
| 352 | void operator()(sdbusplus::bus::bus&, |
| 353 | sdbusplus::message::message& msg, |
| 354 | Zone& zone) const |
| 355 | { |
| 356 | if (msg) |
| 357 | { |
| 358 | std::vector<std::string> intfs; |
| 359 | sdbusplus::message::object_path op; |
| 360 | |
| 361 | msg.read(op); |
| 362 | if (static_cast<const std::string&>(op) != _path) |
| 363 | { |
| 364 | // Object path does not match this handler's path |
| 365 | return; |
| 366 | } |
| 367 | |
| 368 | msg.read(intfs); |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 369 | auto itIntf = std::find(intfs.begin(), intfs.end(), _intf); |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 370 | if (itIntf == intfs.cend()) |
| 371 | { |
| 372 | // Interface not found on this handler's path |
| 373 | return; |
| 374 | } |
| 375 | |
| 376 | _handler(zone); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | private: |
| 381 | const char* _path; |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 382 | const char* _intf; |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 383 | U _handler; |
| 384 | }; |
| 385 | |
| 386 | /** |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 387 | * @brief Used to process a Dbus interfaces removed signal event |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 388 | * |
| 389 | * @param[in] path - Object path |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 390 | * @param[in] intf - Object interface |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 391 | * @param[in] handler - Handler function to perform |
| 392 | * |
| 393 | * @tparam U - The type of the handler |
| 394 | */ |
| 395 | template <typename U> |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 396 | auto interfacesRemoved(const char* path, |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 397 | const char* intf, |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 398 | U&& handler) |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 399 | { |
Matthew Barth | 469d136 | 2018-10-11 14:10:47 -0500 | [diff] [blame^] | 400 | return InterfacesRemoved<U>(path, |
| 401 | intf, |
| 402 | std::forward<U>(handler)); |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 406 | * @struct Name Owner |
| 407 | * @brief A functor for Dbus name owner signals and methods |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 408 | * |
| 409 | * @tparam U - The type of the handler |
| 410 | */ |
| 411 | template <typename U> |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 412 | struct NameOwner |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 413 | { |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 414 | NameOwner() = delete; |
| 415 | ~NameOwner() = default; |
| 416 | NameOwner(const NameOwner&) = default; |
| 417 | NameOwner& operator=(const NameOwner&) = default; |
| 418 | NameOwner(NameOwner&&) = default; |
| 419 | NameOwner& operator=(NameOwner&&) = default; |
| 420 | explicit NameOwner(U&& handler) : |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 421 | _handler(std::forward<U>(handler)) { } |
| 422 | |
| 423 | /** @brief Run signal handler function |
| 424 | * |
| 425 | * Extract the name owner from the NameOwnerChanged |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 426 | * message and run the handler function. |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 427 | */ |
| 428 | void operator()(sdbusplus::bus::bus& bus, |
| 429 | sdbusplus::message::message& msg, |
| 430 | Zone& zone) const |
| 431 | { |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 432 | std::string name; |
| 433 | bool hasOwner = false; |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 434 | if (msg) |
| 435 | { |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 436 | // Handle NameOwnerChanged signals |
| 437 | msg.read(name); |
| 438 | |
| 439 | std::string oldOwn; |
| 440 | msg.read(oldOwn); |
| 441 | |
| 442 | std::string newOwn; |
| 443 | msg.read(newOwn); |
| 444 | if (!newOwn.empty()) |
| 445 | { |
| 446 | hasOwner = true; |
| 447 | } |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 448 | _handler(zone, name, hasOwner); |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 449 | } |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 450 | } |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 451 | |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 452 | void operator()(Zone& zone, |
| 453 | const Group& group) const |
| 454 | { |
| 455 | std::string name = ""; |
| 456 | bool hasOwner = false; |
| 457 | std::for_each( |
| 458 | group.begin(), |
| 459 | group.end(), |
| 460 | [&zone, &group, &name, &hasOwner, handler = std::move(_handler)]( |
| 461 | auto const& member) |
| 462 | { |
| 463 | auto path = std::get<pathPos>(member); |
| 464 | auto intf = std::get<intfPos>(member); |
| 465 | try |
| 466 | { |
| 467 | auto servName = zone.getService(path, intf); |
| 468 | if (name != servName) |
| 469 | { |
| 470 | name = servName; |
| 471 | hasOwner = util::SDBusPlus::callMethodAndRead<bool>( |
| 472 | zone.getBus(), |
| 473 | "org.freedesktop.DBus", |
| 474 | "/org/freedesktop/DBus", |
| 475 | "org.freedesktop.DBus", |
| 476 | "NameHasOwner", |
| 477 | name); |
| 478 | // Update service name owner state list of a group |
| 479 | handler(zone, name, hasOwner); |
| 480 | } |
| 481 | } |
| 482 | catch (const util::DBusMethodError& e) |
| 483 | { |
| 484 | // Failed to get service name owner state |
| 485 | name = ""; |
| 486 | hasOwner = false; |
| 487 | } |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 488 | } |
| 489 | ); |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | private: |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 493 | U _handler; |
| 494 | }; |
| 495 | |
| 496 | /** |
| 497 | * @brief Used to process a Dbus name owner changed signal event |
| 498 | * |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 499 | * @param[in] handler - Handler function to perform |
| 500 | * |
| 501 | * @tparam U - The type of the handler |
| 502 | * |
| 503 | * @return - The NameOwnerChanged signal struct |
| 504 | */ |
| 505 | template <typename U> |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 506 | auto nameOwnerChanged(U&& handler) |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 507 | { |
Matthew Barth | 926df66 | 2018-10-09 09:51:12 -0500 | [diff] [blame] | 508 | return NameOwner<U>(std::forward<U>(handler)); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * @brief Used to process the init of a name owner event |
| 513 | * |
| 514 | * @param[in] handler - Handler function to perform |
| 515 | * |
| 516 | * @tparam U - The type of the handler |
| 517 | * |
| 518 | * @return - The NameOwnerChanged signal struct |
| 519 | */ |
| 520 | template <typename U> |
| 521 | auto nameHasOwner(U&& handler) |
| 522 | { |
| 523 | return NameOwner<U>(std::forward<U>(handler)); |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 524 | } |
| 525 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 526 | } // namespace control |
| 527 | } // namespace fan |
| 528 | } // namespace phosphor |