13cc348ceSShawn McCarney /**
23cc348ceSShawn McCarney  * Copyright © 2024 IBM Corporation
33cc348ceSShawn McCarney  *
43cc348ceSShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
53cc348ceSShawn McCarney  * you may not use this file except in compliance with the License.
63cc348ceSShawn McCarney  * You may obtain a copy of the License at
73cc348ceSShawn McCarney  *
83cc348ceSShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
93cc348ceSShawn McCarney  *
103cc348ceSShawn McCarney  * Unless required by applicable law or agreed to in writing, software
113cc348ceSShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
123cc348ceSShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133cc348ceSShawn McCarney  * See the License for the specific language governing permissions and
143cc348ceSShawn McCarney  * limitations under the License.
153cc348ceSShawn McCarney  */
163cc348ceSShawn McCarney #pragma once
173cc348ceSShawn McCarney 
183cc348ceSShawn McCarney #include "dbus_interfaces_finder.hpp"
193cc348ceSShawn McCarney #include "utility.hpp"
203cc348ceSShawn McCarney 
213cc348ceSShawn McCarney #include <sdbusplus/bus.hpp>
223cc348ceSShawn McCarney 
233cc348ceSShawn McCarney #include <functional>
241838dbf9SShawn McCarney #include <memory>
253cc348ceSShawn McCarney #include <string>
263cc348ceSShawn McCarney #include <vector>
273cc348ceSShawn McCarney 
283cc348ceSShawn McCarney namespace phosphor::power::util
293cc348ceSShawn McCarney {
303cc348ceSShawn McCarney 
313cc348ceSShawn McCarney /**
323cc348ceSShawn McCarney  * @class CompatibleSystemTypesFinder
333cc348ceSShawn McCarney  *
343cc348ceSShawn McCarney  * Class that finds the compatible system types for the current system.
353cc348ceSShawn McCarney  *
363cc348ceSShawn McCarney  * The compatible system types are in a list ordered from most to least
373cc348ceSShawn McCarney  * specific.
383cc348ceSShawn McCarney  *
393cc348ceSShawn McCarney  * Example:
403cc348ceSShawn McCarney  *   - com.acme.Hardware.Chassis.Model.MegaServer4CPU
413cc348ceSShawn McCarney  *   - com.acme.Hardware.Chassis.Model.MegaServer
423cc348ceSShawn McCarney  *   - com.acme.Hardware.Chassis.Model.Server
433cc348ceSShawn McCarney  *
443cc348ceSShawn McCarney  * When a list of compatible system types is found, the callback function
453cc348ceSShawn McCarney  * specified in the constructor is called.  This function will be called
463cc348ceSShawn McCarney  * multiple times if multiple lists of compatible system types are found.
473cc348ceSShawn McCarney  */
483cc348ceSShawn McCarney class CompatibleSystemTypesFinder
493cc348ceSShawn McCarney {
503cc348ceSShawn McCarney   public:
513cc348ceSShawn McCarney     // Specify which compiler-generated methods we want
523cc348ceSShawn McCarney     CompatibleSystemTypesFinder() = delete;
533cc348ceSShawn McCarney     CompatibleSystemTypesFinder(const CompatibleSystemTypesFinder&) = delete;
543cc348ceSShawn McCarney     CompatibleSystemTypesFinder(CompatibleSystemTypesFinder&&) = delete;
553cc348ceSShawn McCarney     CompatibleSystemTypesFinder&
563cc348ceSShawn McCarney         operator=(const CompatibleSystemTypesFinder&) = delete;
573cc348ceSShawn McCarney     CompatibleSystemTypesFinder&
583cc348ceSShawn McCarney         operator=(CompatibleSystemTypesFinder&&) = delete;
593cc348ceSShawn McCarney     ~CompatibleSystemTypesFinder() = default;
603cc348ceSShawn McCarney 
613cc348ceSShawn McCarney     /**
623cc348ceSShawn McCarney      * Callback function that is called when a list of compatible system types
633cc348ceSShawn McCarney      * is found.
643cc348ceSShawn McCarney      *
653cc348ceSShawn McCarney      * @param compatibleSystemTypes Compatible system types for the current
663cc348ceSShawn McCarney      *                              system ordered from most to least specific
673cc348ceSShawn McCarney      */
683cc348ceSShawn McCarney     using Callback = std::function<void(
693cc348ceSShawn McCarney         const std::vector<std::string>& compatibleSystemTypes)>;
703cc348ceSShawn McCarney 
713cc348ceSShawn McCarney     /**
723cc348ceSShawn McCarney      * Constructor.
733cc348ceSShawn McCarney      *
741838dbf9SShawn McCarney      * Note: The callback function may be called immediately by this
751838dbf9SShawn McCarney      * constructor.  For this reason, do not use this constructor in the
761838dbf9SShawn McCarney      * initialization list of constructors in other classes.  Otherwise the
771838dbf9SShawn McCarney      * callback may be called before the other class is fully initialized,
781838dbf9SShawn McCarney      * leading to unpredictable behavior.
791838dbf9SShawn McCarney      *
803cc348ceSShawn McCarney      * @param bus D-Bus bus object
813cc348ceSShawn McCarney      * @param callback Callback function that is called each time a list of
823cc348ceSShawn McCarney      *                 compatible system types is found
833cc348ceSShawn McCarney      */
843cc348ceSShawn McCarney     explicit CompatibleSystemTypesFinder(sdbusplus::bus_t& bus,
853cc348ceSShawn McCarney                                          Callback callback);
863cc348ceSShawn McCarney 
873cc348ceSShawn McCarney     /**
88*8b098b97SShawn McCarney      * Refind all compatible system types for the current system.
89*8b098b97SShawn McCarney      *
90*8b098b97SShawn McCarney      * The callback specified in the constructor will be called for each list of
91*8b098b97SShawn McCarney      * compatible system types found.
92*8b098b97SShawn McCarney      *
93*8b098b97SShawn McCarney      * This method normally does not need to be called.  New lists of compatible
94*8b098b97SShawn McCarney      * system types are automatically detected using an InterfacesAdded
95*8b098b97SShawn McCarney      * listener.  However, this method may be useful if the caller is not
96*8b098b97SShawn McCarney      * currently receiving D-Bus signals (such as within a loop).
97*8b098b97SShawn McCarney      */
refind()98*8b098b97SShawn McCarney     void refind()
99*8b098b97SShawn McCarney     {
100*8b098b97SShawn McCarney         interfaceFinder->refind();
101*8b098b97SShawn McCarney     }
102*8b098b97SShawn McCarney 
103*8b098b97SShawn McCarney     /**
1043cc348ceSShawn McCarney      * Callback function that is called when a Compatible interface is found.
1053cc348ceSShawn McCarney      *
1063cc348ceSShawn McCarney      * @param path D-Bus object path that implements the interface
1073cc348ceSShawn McCarney      * @param interface D-Bus interface that was found
1083cc348ceSShawn McCarney      * @param properties Properties of the D-Bus interface
1093cc348ceSShawn McCarney      */
1103cc348ceSShawn McCarney     void interfaceFoundCallback(const std::string& path,
1113cc348ceSShawn McCarney                                 const std::string& interface,
1123cc348ceSShawn McCarney                                 const DbusPropertyMap& properties);
1133cc348ceSShawn McCarney 
1143cc348ceSShawn McCarney   private:
1153cc348ceSShawn McCarney     /**
1163cc348ceSShawn McCarney      * Callback function that is called each time a list of compatible system
1173cc348ceSShawn McCarney      * types is found.
1183cc348ceSShawn McCarney      */
1193cc348ceSShawn McCarney     Callback callback;
1203cc348ceSShawn McCarney 
1213cc348ceSShawn McCarney     /**
1223cc348ceSShawn McCarney      * Class used to find instances of the D-Bus Compatible interface.
1233cc348ceSShawn McCarney      */
1241838dbf9SShawn McCarney     std::unique_ptr<DBusInterfacesFinder> interfaceFinder;
1253cc348ceSShawn McCarney };
1263cc348ceSShawn McCarney 
1273cc348ceSShawn McCarney } // namespace phosphor::power::util
128