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