Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "types.hpp" |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 4 | #include "sdbusplus.hpp" |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 5 | #include <phosphor-logging/log.hpp> |
| 6 | |
| 7 | namespace phosphor |
| 8 | { |
| 9 | namespace fan |
| 10 | { |
| 11 | namespace control |
| 12 | { |
| 13 | class Zone; |
| 14 | |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 15 | using namespace phosphor::fan; |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 16 | using namespace sdbusplus::bus::match; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 17 | using namespace phosphor::logging; |
| 18 | |
| 19 | /** |
| 20 | * @brief Create a handler function object |
| 21 | * |
| 22 | * @param[in] handler - The handler being created |
| 23 | * |
| 24 | * @return - The created handler function object |
| 25 | */ |
| 26 | template <typename T> |
| 27 | auto make_handler(T&& handler) |
| 28 | { |
| 29 | return Handler(std::forward<T>(handler)); |
| 30 | } |
| 31 | |
| 32 | /** |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 33 | * @brief Create an action function object |
| 34 | * |
| 35 | * @param[in] action - The action being created |
| 36 | * |
| 37 | * @return - The created action function object |
| 38 | */ |
| 39 | template <typename T> |
| 40 | auto make_action(T&& action) |
| 41 | { |
| 42 | return Action(std::forward<T>(action)); |
| 43 | } |
| 44 | |
| 45 | /** |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 46 | * @struct Property Changed |
| 47 | * @brief A match filter functor for Dbus property value changed signals |
| 48 | * |
| 49 | * @tparam T - The type of the property value |
| 50 | * @tparam U - The type of the handler |
| 51 | */ |
| 52 | template <typename T, typename U> |
| 53 | struct PropertyChanged |
| 54 | { |
| 55 | PropertyChanged() = delete; |
| 56 | ~PropertyChanged() = default; |
| 57 | PropertyChanged(const PropertyChanged&) = default; |
| 58 | PropertyChanged& operator=(const PropertyChanged&) = default; |
| 59 | PropertyChanged(PropertyChanged&&) = default; |
| 60 | PropertyChanged& operator=(PropertyChanged&&) = default; |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 61 | PropertyChanged(const char* path, |
| 62 | const char* iface, |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 63 | const char* property, |
| 64 | U&& handler) : |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 65 | _path(path), |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 66 | _iface(iface), |
| 67 | _property(property), |
| 68 | _handler(std::forward<U>(handler)) { } |
| 69 | |
| 70 | /** @brief Run signal handler function |
| 71 | * |
| 72 | * Extract the property from the PropertiesChanged |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 73 | * message (or read the property when the message is null) |
| 74 | * and run the handler function. |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 75 | */ |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 76 | void operator()(sdbusplus::bus::bus& bus, |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 77 | sdbusplus::message::message& msg, |
| 78 | Zone& zone) const |
| 79 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 80 | if (msg) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 81 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 82 | std::map<std::string, sdbusplus::message::variant<T>> properties; |
Matthew Barth | d5cfdbe | 2017-11-14 15:29:50 -0600 | [diff] [blame] | 83 | std::string iface; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 84 | |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 85 | msg.read(iface); |
Matthew Barth | d5cfdbe | 2017-11-14 15:29:50 -0600 | [diff] [blame] | 86 | if (iface != _iface) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 87 | { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | msg.read(properties); |
| 92 | auto it = properties.find(_property); |
| 93 | if (it == properties.cend()) |
| 94 | { |
| 95 | log<level::ERR>("Unable to find property on interface", |
| 96 | entry("PROPERTY=%s", _property), |
Matthew Barth | e65f617 | 2017-12-11 13:36:02 -0600 | [diff] [blame] | 97 | entry("INTERFACE=%s", _iface), |
| 98 | entry("PATH=%s", _path)); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | |
| 102 | _handler(zone, std::forward<T>(it->second.template get<T>())); |
| 103 | } |
| 104 | else |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 105 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 106 | try |
| 107 | { |
Matthew Barth | c72b891 | 2018-01-19 17:28:18 -0600 | [diff] [blame] | 108 | auto service = zone.getService(_path, _iface); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 109 | auto val = util::SDBusPlus::getProperty<T>(bus, |
Matthew Barth | c72b891 | 2018-01-19 17:28:18 -0600 | [diff] [blame] | 110 | service, |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 111 | _path, |
| 112 | _iface, |
| 113 | _property); |
| 114 | _handler(zone, std::forward<T>(val)); |
| 115 | } |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 116 | catch (const util::DBusError& e) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 117 | { |
| 118 | // Property will not be used unless a property changed |
| 119 | // signal message is received for this property. |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 120 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 121 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private: |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 125 | const char* _path; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 126 | const char* _iface; |
| 127 | const char* _property; |
| 128 | U _handler; |
| 129 | }; |
| 130 | |
| 131 | /** |
| 132 | * @brief Used to process a Dbus property changed signal event |
| 133 | * |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 134 | * @param[in] path - Object path |
| 135 | * @param[in] iface - Object interface |
| 136 | * @param[in] property - Object property |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 137 | * @param[in] handler - Handler function to perform |
| 138 | * |
| 139 | * @tparam T - The type of the property |
| 140 | * @tparam U - The type of the handler |
| 141 | */ |
| 142 | template <typename T, typename U> |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 143 | auto propertySignal(const char* path, |
| 144 | const char* iface, |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 145 | const char* property, |
| 146 | U&& handler) |
| 147 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 148 | return PropertyChanged<T, U>(path, |
| 149 | iface, |
| 150 | property, |
| 151 | std::forward<U>(handler)); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 154 | /** |
| 155 | * @struct Interface Added |
| 156 | * @brief A match filter functor for Dbus interface added signals |
| 157 | * |
| 158 | * @tparam T - The type of the property value |
| 159 | * @tparam U - The type of the handler |
| 160 | */ |
| 161 | template <typename T, typename U> |
| 162 | struct InterfaceAdded |
| 163 | { |
| 164 | InterfaceAdded() = delete; |
| 165 | ~InterfaceAdded() = default; |
| 166 | InterfaceAdded(const InterfaceAdded&) = default; |
| 167 | InterfaceAdded& operator=(const InterfaceAdded&) = default; |
| 168 | InterfaceAdded(InterfaceAdded&&) = default; |
| 169 | InterfaceAdded& operator=(InterfaceAdded&&) = default; |
| 170 | InterfaceAdded(const char* path, |
| 171 | const char* iface, |
| 172 | const char* property, |
| 173 | U&& handler) : |
| 174 | _path(path), |
| 175 | _iface(iface), |
| 176 | _property(property), |
| 177 | _handler(std::forward<U>(handler)) { } |
| 178 | |
| 179 | /** @brief Run signal handler function |
| 180 | * |
| 181 | * Extract the property from the InterfacesAdded |
| 182 | * message and run the handler function. |
| 183 | */ |
| 184 | void operator()(sdbusplus::bus::bus&, |
| 185 | sdbusplus::message::message& msg, |
| 186 | Zone& zone) const |
| 187 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 188 | if (msg) |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 189 | { |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 190 | std::map<std::string, |
| 191 | std::map<std::string, |
| 192 | sdbusplus::message::variant<T>>> intfProp; |
| 193 | sdbusplus::message::object_path op; |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 194 | |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 195 | msg.read(op); |
Matthew Barth | d5cfdbe | 2017-11-14 15:29:50 -0600 | [diff] [blame] | 196 | if (static_cast<const std::string&>(op) != _path) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 197 | { |
| 198 | // Object path does not match this handler's path |
| 199 | return; |
| 200 | } |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 201 | |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 202 | msg.read(intfProp); |
| 203 | auto itIntf = intfProp.find(_iface); |
| 204 | if (itIntf == intfProp.cend()) |
| 205 | { |
| 206 | // Interface not found on this handler's path |
| 207 | return; |
| 208 | } |
| 209 | auto itProp = itIntf->second.find(_property); |
| 210 | if (itProp == itIntf->second.cend()) |
| 211 | { |
| 212 | // Property not found on this handler's path |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | _handler(zone, std::forward<T>(itProp->second.template get<T>())); |
| 217 | } |
Matthew Barth | eb639c5 | 2017-08-04 09:43:11 -0500 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | private: |
| 221 | const char* _path; |
| 222 | const char* _iface; |
| 223 | const char* _property; |
| 224 | U _handler; |
| 225 | }; |
| 226 | |
| 227 | /** |
| 228 | * @brief Used to process a Dbus interface added signal event |
| 229 | * |
| 230 | * @param[in] path - Object path |
| 231 | * @param[in] iface - Object interface |
| 232 | * @param[in] property - Object property |
| 233 | * @param[in] handler - Handler function to perform |
| 234 | * |
| 235 | * @tparam T - The type of the property |
| 236 | * @tparam U - The type of the handler |
| 237 | */ |
| 238 | template <typename T, typename U> |
| 239 | auto objectSignal(const char* path, |
| 240 | const char* iface, |
| 241 | const char* property, |
| 242 | U&& handler) |
| 243 | { |
| 244 | return InterfaceAdded<T, U>(path, |
| 245 | iface, |
| 246 | property, |
| 247 | std::forward<U>(handler)); |
| 248 | } |
| 249 | |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 250 | /** |
Matthew Barth | 1499a5c | 2018-03-20 15:52:33 -0500 | [diff] [blame] | 251 | * @struct Interface Removed |
| 252 | * @brief A match filter functor for Dbus interface removed signals |
| 253 | * |
| 254 | * @tparam U - The type of the handler |
| 255 | */ |
| 256 | template <typename U> |
| 257 | struct InterfaceRemoved |
| 258 | { |
| 259 | InterfaceRemoved() = delete; |
| 260 | ~InterfaceRemoved() = default; |
| 261 | InterfaceRemoved(const InterfaceRemoved&) = default; |
| 262 | InterfaceRemoved& operator=(const InterfaceRemoved&) = default; |
| 263 | InterfaceRemoved(InterfaceRemoved&&) = default; |
| 264 | InterfaceRemoved& operator=(InterfaceRemoved&&) = default; |
| 265 | InterfaceRemoved(const char* path, |
| 266 | const char* iface, |
| 267 | U&& handler) : |
| 268 | _path(path), |
| 269 | _iface(iface), |
| 270 | _handler(std::forward<U>(handler)) { } |
| 271 | |
| 272 | /** @brief Run signal handler function |
| 273 | * |
| 274 | * Extract the property from the InterfacesRemoved |
| 275 | * message and run the handler function. |
| 276 | */ |
| 277 | void operator()(sdbusplus::bus::bus&, |
| 278 | sdbusplus::message::message& msg, |
| 279 | Zone& zone) const |
| 280 | { |
| 281 | if (msg) |
| 282 | { |
| 283 | std::vector<std::string> intfs; |
| 284 | sdbusplus::message::object_path op; |
| 285 | |
| 286 | msg.read(op); |
| 287 | if (static_cast<const std::string&>(op) != _path) |
| 288 | { |
| 289 | // Object path does not match this handler's path |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | msg.read(intfs); |
| 294 | auto itIntf = std::find(intfs.begin(), intfs.end(), _iface); |
| 295 | if (itIntf == intfs.cend()) |
| 296 | { |
| 297 | // Interface not found on this handler's path |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | _handler(zone); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | private: |
| 306 | const char* _path; |
| 307 | const char* _iface; |
| 308 | U _handler; |
| 309 | }; |
| 310 | |
| 311 | /** |
| 312 | * @brief Used to process a Dbus interface removed signal event |
| 313 | * |
| 314 | * @param[in] path - Object path |
| 315 | * @param[in] iface - Object interface |
| 316 | * @param[in] handler - Handler function to perform |
| 317 | * |
| 318 | * @tparam U - The type of the handler |
| 319 | */ |
| 320 | template <typename U> |
| 321 | auto objectSignal(const char* path, |
| 322 | const char* iface, |
| 323 | U&& handler) |
| 324 | { |
| 325 | return InterfaceRemoved<U>(path, |
| 326 | iface, |
| 327 | std::forward<U>(handler)); |
| 328 | } |
| 329 | |
| 330 | /** |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 331 | * @struct Name Owner Changed |
| 332 | * @brief A match filter functor for Dbus name owner changed signals |
| 333 | * |
| 334 | * @tparam U - The type of the handler |
| 335 | */ |
| 336 | template <typename U> |
| 337 | struct NameOwnerChanged |
| 338 | { |
| 339 | NameOwnerChanged() = delete; |
| 340 | ~NameOwnerChanged() = default; |
| 341 | NameOwnerChanged(const NameOwnerChanged&) = default; |
| 342 | NameOwnerChanged& operator=(const NameOwnerChanged&) = default; |
| 343 | NameOwnerChanged(NameOwnerChanged&&) = default; |
| 344 | NameOwnerChanged& operator=(NameOwnerChanged&&) = default; |
| 345 | NameOwnerChanged(const char* path, |
| 346 | const char* iface, |
| 347 | U&& handler) : |
| 348 | _path(path), |
| 349 | _iface(iface), |
| 350 | _handler(std::forward<U>(handler)) { } |
| 351 | |
| 352 | /** @brief Run signal handler function |
| 353 | * |
| 354 | * Extract the name owner from the NameOwnerChanged |
| 355 | * message (or read the name owner when the message is null) |
| 356 | * and run the handler function. |
| 357 | */ |
| 358 | void operator()(sdbusplus::bus::bus& bus, |
| 359 | sdbusplus::message::message& msg, |
| 360 | Zone& zone) const |
| 361 | { |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 362 | std::string name; |
| 363 | bool hasOwner = false; |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 364 | if (msg) |
| 365 | { |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 366 | // Handle NameOwnerChanged signals |
| 367 | msg.read(name); |
| 368 | |
| 369 | std::string oldOwn; |
| 370 | msg.read(oldOwn); |
| 371 | |
| 372 | std::string newOwn; |
| 373 | msg.read(newOwn); |
| 374 | if (!newOwn.empty()) |
| 375 | { |
| 376 | hasOwner = true; |
| 377 | } |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 378 | } |
| 379 | else |
| 380 | { |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 381 | try |
| 382 | { |
| 383 | // Initialize NameOwnerChanged data store with service name |
Matthew Barth | c72b891 | 2018-01-19 17:28:18 -0600 | [diff] [blame] | 384 | name = zone.getService(_path, _iface); |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 385 | hasOwner = util::SDBusPlus::callMethodAndRead<bool>( |
| 386 | bus, |
| 387 | "org.freedesktop.DBus", |
| 388 | "/org/freedesktop/DBus", |
| 389 | "org.freedesktop.DBus", |
| 390 | "NameHasOwner", |
| 391 | name); |
| 392 | } |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 393 | catch (const util::DBusMethodError& e) |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 394 | { |
| 395 | // Failed to get service name owner state |
| 396 | hasOwner = false; |
| 397 | } |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 398 | } |
Matthew Barth | 5a30257 | 2017-10-03 11:27:06 -0500 | [diff] [blame] | 399 | |
| 400 | _handler(zone, name, hasOwner); |
Matthew Barth | 8fa02da | 2017-09-28 12:18:20 -0500 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | private: |
| 404 | const char* _path; |
| 405 | const char* _iface; |
| 406 | U _handler; |
| 407 | }; |
| 408 | |
| 409 | /** |
| 410 | * @brief Used to process a Dbus name owner changed signal event |
| 411 | * |
| 412 | * @param[in] path - Object path |
| 413 | * @param[in] iface - Object interface |
| 414 | * @param[in] handler - Handler function to perform |
| 415 | * |
| 416 | * @tparam U - The type of the handler |
| 417 | * |
| 418 | * @return - The NameOwnerChanged signal struct |
| 419 | */ |
| 420 | template <typename U> |
| 421 | auto ownerSignal(const char* path, |
| 422 | const char* iface, |
| 423 | U&& handler) |
| 424 | { |
| 425 | return NameOwnerChanged<U>(path, |
| 426 | iface, |
| 427 | std::forward<U>(handler)); |
| 428 | } |
| 429 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 430 | } // namespace control |
| 431 | } // namespace fan |
| 432 | } // namespace phosphor |