xref: /openbmc/phosphor-power/phosphor-power-sequencer/src/device_finder.hpp (revision 1838dbf934e4788c613b8b106f2d9cc1382d30b9)
1452de22eSShawn McCarney /**
2452de22eSShawn McCarney  * Copyright © 2024 IBM Corporation
3452de22eSShawn McCarney  *
4452de22eSShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
5452de22eSShawn McCarney  * you may not use this file except in compliance with the License.
6452de22eSShawn McCarney  * You may obtain a copy of the License at
7452de22eSShawn McCarney  *
8452de22eSShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
9452de22eSShawn McCarney  *
10452de22eSShawn McCarney  * Unless required by applicable law or agreed to in writing, software
11452de22eSShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
12452de22eSShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13452de22eSShawn McCarney  * See the License for the specific language governing permissions and
14452de22eSShawn McCarney  * limitations under the License.
15452de22eSShawn McCarney  */
16452de22eSShawn McCarney #pragma once
17452de22eSShawn McCarney 
18452de22eSShawn McCarney #include "dbus_interfaces_finder.hpp"
19452de22eSShawn McCarney #include "utility.hpp"
20452de22eSShawn McCarney 
21452de22eSShawn McCarney #include <sdbusplus/bus.hpp>
22452de22eSShawn McCarney 
23452de22eSShawn McCarney #include <cstdint>
24452de22eSShawn McCarney #include <functional>
25*1838dbf9SShawn McCarney #include <memory>
26452de22eSShawn McCarney #include <string>
27452de22eSShawn McCarney 
28452de22eSShawn McCarney namespace phosphor::power::sequencer
29452de22eSShawn McCarney {
30452de22eSShawn McCarney 
31452de22eSShawn McCarney using DbusVariant = phosphor::power::util::DbusVariant;
32452de22eSShawn McCarney using DbusPropertyMap = phosphor::power::util::DbusPropertyMap;
33452de22eSShawn McCarney using DBusInterfacesFinder = phosphor::power::util::DBusInterfacesFinder;
34452de22eSShawn McCarney 
35452de22eSShawn McCarney /**
36452de22eSShawn McCarney  * Power sequencer device properties.
37452de22eSShawn McCarney  */
38452de22eSShawn McCarney struct DeviceProperties
39452de22eSShawn McCarney {
40452de22eSShawn McCarney     std::string type;
41452de22eSShawn McCarney     std::string name;
42452de22eSShawn McCarney     uint8_t bus;
43452de22eSShawn McCarney     uint16_t address;
44452de22eSShawn McCarney };
45452de22eSShawn McCarney 
46452de22eSShawn McCarney /**
47452de22eSShawn McCarney  * @class DeviceFinder
48452de22eSShawn McCarney  *
49452de22eSShawn McCarney  * Class that finds power sequencer devices in the system.
50452de22eSShawn McCarney  *
51452de22eSShawn McCarney  * When a device is found, the callback function specified in the constructor is
52452de22eSShawn McCarney  * called.  This function will be called multiple times if multiple devices are
53452de22eSShawn McCarney  * found.
54452de22eSShawn McCarney  */
55452de22eSShawn McCarney class DeviceFinder
56452de22eSShawn McCarney {
57452de22eSShawn McCarney   public:
58452de22eSShawn McCarney     // Specify which compiler-generated methods we want
59452de22eSShawn McCarney     DeviceFinder() = delete;
60452de22eSShawn McCarney     DeviceFinder(const DeviceFinder&) = delete;
61452de22eSShawn McCarney     DeviceFinder(DeviceFinder&&) = delete;
62452de22eSShawn McCarney     DeviceFinder& operator=(const DeviceFinder&) = delete;
63452de22eSShawn McCarney     DeviceFinder& operator=(DeviceFinder&&) = delete;
64452de22eSShawn McCarney     ~DeviceFinder() = default;
65452de22eSShawn McCarney 
66452de22eSShawn McCarney     /**
67452de22eSShawn McCarney      * Callback function that is called when a power sequencer device is found.
68452de22eSShawn McCarney      *
69452de22eSShawn McCarney      * @param device Device that was found
70452de22eSShawn McCarney      */
71452de22eSShawn McCarney     using Callback = std::function<void(const DeviceProperties& device)>;
72452de22eSShawn McCarney 
73452de22eSShawn McCarney     /**
74452de22eSShawn McCarney      * Constructor.
75452de22eSShawn McCarney      *
76*1838dbf9SShawn McCarney      * Note: The callback function may be called immediately by this
77*1838dbf9SShawn McCarney      * constructor.  For this reason, do not use this constructor in the
78*1838dbf9SShawn McCarney      * initialization list of constructors in other classes.  Otherwise the
79*1838dbf9SShawn McCarney      * callback may be called before the other class is fully initialized,
80*1838dbf9SShawn McCarney      * leading to unpredictable behavior.
81*1838dbf9SShawn McCarney      *
82452de22eSShawn McCarney      * @param bus D-Bus bus object
83452de22eSShawn McCarney      * @param callback Callback function that is called each time a power
84452de22eSShawn McCarney      *                 sequencer device is found
85452de22eSShawn McCarney      */
86452de22eSShawn McCarney     explicit DeviceFinder(sdbusplus::bus_t& bus, Callback callback);
87452de22eSShawn McCarney 
88452de22eSShawn McCarney     /**
89452de22eSShawn McCarney      * Callback function that is called when a D-Bus interface is found that
90452de22eSShawn McCarney      * contains power sequencer device properties.
91452de22eSShawn McCarney      *
92452de22eSShawn McCarney      * @param path D-Bus object path that implements the interface
93452de22eSShawn McCarney      * @param interface D-Bus interface that was found
94452de22eSShawn McCarney      * @param properties Properties of the D-Bus interface
95452de22eSShawn McCarney      */
96452de22eSShawn McCarney     void interfaceFoundCallback(const std::string& path,
97452de22eSShawn McCarney                                 const std::string& interface,
98452de22eSShawn McCarney                                 const DbusPropertyMap& properties);
99452de22eSShawn McCarney 
100452de22eSShawn McCarney   private:
101452de22eSShawn McCarney     /**
102452de22eSShawn McCarney      * Returns the value of the D-Bus property with the specified name.
103452de22eSShawn McCarney      *
104452de22eSShawn McCarney      * Throws an exception if the property was not found.
105452de22eSShawn McCarney      *
106452de22eSShawn McCarney      * @param properties D-Bus interface properties
107452de22eSShawn McCarney      * @param propertyName D-Bus property name
108452de22eSShawn McCarney      * @return Property value
109452de22eSShawn McCarney      */
110452de22eSShawn McCarney     const DbusVariant& getPropertyValue(const DbusPropertyMap& properties,
111452de22eSShawn McCarney                                         const std::string& propertyName);
112452de22eSShawn McCarney 
113452de22eSShawn McCarney     /**
114452de22eSShawn McCarney      * Callback function that is called each time a power sequencer device is
115452de22eSShawn McCarney      * found.
116452de22eSShawn McCarney      */
117452de22eSShawn McCarney     Callback callback;
118452de22eSShawn McCarney 
119452de22eSShawn McCarney     /**
120452de22eSShawn McCarney      * Class used to find D-Bus interfaces that contain power sequencer device
121452de22eSShawn McCarney      * properties.
122452de22eSShawn McCarney      */
123*1838dbf9SShawn McCarney     std::unique_ptr<DBusInterfacesFinder> interfacesFinder;
124452de22eSShawn McCarney };
125452de22eSShawn McCarney 
126452de22eSShawn McCarney } // namespace phosphor::power::sequencer
127