Shawn McCarney | 452de22 | 2024-05-30 15:23:09 -0500 | [diff] [blame^] | 1 | /** |
| 2 | * Copyright © 2024 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include "dbus_interfaces_finder.hpp" |
| 19 | #include "utility.hpp" |
| 20 | |
| 21 | #include <sdbusplus/bus.hpp> |
| 22 | |
| 23 | #include <cstdint> |
| 24 | #include <functional> |
| 25 | #include <string> |
| 26 | |
| 27 | namespace phosphor::power::sequencer |
| 28 | { |
| 29 | |
| 30 | using DbusVariant = phosphor::power::util::DbusVariant; |
| 31 | using DbusPropertyMap = phosphor::power::util::DbusPropertyMap; |
| 32 | using DBusInterfacesFinder = phosphor::power::util::DBusInterfacesFinder; |
| 33 | |
| 34 | /** |
| 35 | * Power sequencer device properties. |
| 36 | */ |
| 37 | struct DeviceProperties |
| 38 | { |
| 39 | std::string type; |
| 40 | std::string name; |
| 41 | uint8_t bus; |
| 42 | uint16_t address; |
| 43 | }; |
| 44 | |
| 45 | /** |
| 46 | * @class DeviceFinder |
| 47 | * |
| 48 | * Class that finds power sequencer devices in the system. |
| 49 | * |
| 50 | * When a device is found, the callback function specified in the constructor is |
| 51 | * called. This function will be called multiple times if multiple devices are |
| 52 | * found. |
| 53 | */ |
| 54 | class DeviceFinder |
| 55 | { |
| 56 | public: |
| 57 | // Specify which compiler-generated methods we want |
| 58 | DeviceFinder() = delete; |
| 59 | DeviceFinder(const DeviceFinder&) = delete; |
| 60 | DeviceFinder(DeviceFinder&&) = delete; |
| 61 | DeviceFinder& operator=(const DeviceFinder&) = delete; |
| 62 | DeviceFinder& operator=(DeviceFinder&&) = delete; |
| 63 | ~DeviceFinder() = default; |
| 64 | |
| 65 | /** |
| 66 | * Callback function that is called when a power sequencer device is found. |
| 67 | * |
| 68 | * @param device Device that was found |
| 69 | */ |
| 70 | using Callback = std::function<void(const DeviceProperties& device)>; |
| 71 | |
| 72 | /** |
| 73 | * Constructor. |
| 74 | * |
| 75 | * @param bus D-Bus bus object |
| 76 | * @param callback Callback function that is called each time a power |
| 77 | * sequencer device is found |
| 78 | */ |
| 79 | explicit DeviceFinder(sdbusplus::bus_t& bus, Callback callback); |
| 80 | |
| 81 | /** |
| 82 | * Callback function that is called when a D-Bus interface is found that |
| 83 | * contains power sequencer device properties. |
| 84 | * |
| 85 | * @param path D-Bus object path that implements the interface |
| 86 | * @param interface D-Bus interface that was found |
| 87 | * @param properties Properties of the D-Bus interface |
| 88 | */ |
| 89 | void interfaceFoundCallback(const std::string& path, |
| 90 | const std::string& interface, |
| 91 | const DbusPropertyMap& properties); |
| 92 | |
| 93 | private: |
| 94 | /** |
| 95 | * Returns the value of the D-Bus property with the specified name. |
| 96 | * |
| 97 | * Throws an exception if the property was not found. |
| 98 | * |
| 99 | * @param properties D-Bus interface properties |
| 100 | * @param propertyName D-Bus property name |
| 101 | * @return Property value |
| 102 | */ |
| 103 | const DbusVariant& getPropertyValue(const DbusPropertyMap& properties, |
| 104 | const std::string& propertyName); |
| 105 | |
| 106 | /** |
| 107 | * Callback function that is called each time a power sequencer device is |
| 108 | * found. |
| 109 | */ |
| 110 | Callback callback; |
| 111 | |
| 112 | /** |
| 113 | * Class used to find D-Bus interfaces that contain power sequencer device |
| 114 | * properties. |
| 115 | */ |
| 116 | DBusInterfacesFinder interfacesFinder; |
| 117 | }; |
| 118 | |
| 119 | } // namespace phosphor::power::sequencer |