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