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