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