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 <functional>
24 #include <string>
25 #include <vector>
26 
27 namespace phosphor::power::util
28 {
29 
30 /**
31  * @class CompatibleSystemTypesFinder
32  *
33  * Class that finds the compatible system types for the current system.
34  *
35  * The compatible system types are in a list ordered from most to least
36  * specific.
37  *
38  * Example:
39  *   - com.acme.Hardware.Chassis.Model.MegaServer4CPU
40  *   - com.acme.Hardware.Chassis.Model.MegaServer
41  *   - com.acme.Hardware.Chassis.Model.Server
42  *
43  * When a list of compatible system types is found, the callback function
44  * specified in the constructor is called.  This function will be called
45  * multiple times if multiple lists of compatible system types are found.
46  */
47 class CompatibleSystemTypesFinder
48 {
49   public:
50     // Specify which compiler-generated methods we want
51     CompatibleSystemTypesFinder() = delete;
52     CompatibleSystemTypesFinder(const CompatibleSystemTypesFinder&) = delete;
53     CompatibleSystemTypesFinder(CompatibleSystemTypesFinder&&) = delete;
54     CompatibleSystemTypesFinder&
55         operator=(const CompatibleSystemTypesFinder&) = delete;
56     CompatibleSystemTypesFinder&
57         operator=(CompatibleSystemTypesFinder&&) = delete;
58     ~CompatibleSystemTypesFinder() = default;
59 
60     /**
61      * Callback function that is called when a list of compatible system types
62      * is found.
63      *
64      * @param compatibleSystemTypes Compatible system types for the current
65      *                              system ordered from most to least specific
66      */
67     using Callback = std::function<void(
68         const std::vector<std::string>& compatibleSystemTypes)>;
69 
70     /**
71      * Constructor.
72      *
73      * @param bus D-Bus bus object
74      * @param callback Callback function that is called each time a list of
75      *                 compatible system types is found
76      */
77     explicit CompatibleSystemTypesFinder(sdbusplus::bus_t& bus,
78                                          Callback callback);
79 
80     /**
81      * Callback function that is called when a Compatible interface is found.
82      *
83      * @param path D-Bus object path that implements the interface
84      * @param interface D-Bus interface that was found
85      * @param properties Properties of the D-Bus interface
86      */
87     void interfaceFoundCallback(const std::string& path,
88                                 const std::string& interface,
89                                 const DbusPropertyMap& properties);
90 
91   private:
92     /**
93      * Callback function that is called each time a list of compatible system
94      * types is found.
95      */
96     Callback callback;
97 
98     /**
99      * Class used to find instances of the D-Bus Compatible interface.
100      */
101     DBusInterfacesFinder interfaceFinder;
102 };
103 
104 } // namespace phosphor::power::util
105