198f42947SShawn McCarney /**
298f42947SShawn McCarney  * Copyright © 2024 IBM Corporation
398f42947SShawn McCarney  *
498f42947SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
598f42947SShawn McCarney  * you may not use this file except in compliance with the License.
698f42947SShawn McCarney  * You may obtain a copy of the License at
798f42947SShawn McCarney  *
898f42947SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
998f42947SShawn McCarney  *
1098f42947SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
1198f42947SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
1298f42947SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1398f42947SShawn McCarney  * See the License for the specific language governing permissions and
1498f42947SShawn McCarney  * limitations under the License.
1598f42947SShawn McCarney  */
1698f42947SShawn McCarney #pragma once
1798f42947SShawn McCarney 
1898f42947SShawn McCarney #include "utility.hpp"
1998f42947SShawn McCarney 
2098f42947SShawn McCarney #include <sdbusplus/bus.hpp>
2198f42947SShawn McCarney #include <sdbusplus/bus/match.hpp>
2298f42947SShawn McCarney 
2398f42947SShawn McCarney #include <functional>
2498f42947SShawn McCarney #include <string>
2598f42947SShawn McCarney #include <vector>
2698f42947SShawn McCarney 
2798f42947SShawn McCarney namespace phosphor::power::util
2898f42947SShawn McCarney {
2998f42947SShawn McCarney 
3098f42947SShawn McCarney /**
3198f42947SShawn McCarney  * @class DBusInterfacesFinder
3298f42947SShawn McCarney  *
3398f42947SShawn McCarney  * Class that finds instances of one or more D-Bus interfaces.
3498f42947SShawn McCarney  *
3598f42947SShawn McCarney  * A D-Bus service name and one or more D-Bus interfaces are specified in the
3698f42947SShawn McCarney  * constructor.  The class finds instances of those interfaces that are owned by
3798f42947SShawn McCarney  * the service.
3898f42947SShawn McCarney  *
3998f42947SShawn McCarney  * The class finds the instances using two different methods:
4098f42947SShawn McCarney  * - Registers an InterfacesAdded listener for the specified service.  Class is
4198f42947SShawn McCarney  *   notified when a new interface instance is created on D-Bus.
4298f42947SShawn McCarney  * - Queries the ObjectMapper to find interface instances that already exist.
4398f42947SShawn McCarney  *
4498f42947SShawn McCarney  * Utilizing both methods allows this class to be used before, during, or after
4598f42947SShawn McCarney  * the service has created the interface instances.
4698f42947SShawn McCarney  *
4798f42947SShawn McCarney  * When an interface instance is found, the callback function specified in the
4898f42947SShawn McCarney  * constructor is called.  This function will be called multiple times if
4998f42947SShawn McCarney  * multiple instances are found.
5098f42947SShawn McCarney  */
5198f42947SShawn McCarney class DBusInterfacesFinder
5298f42947SShawn McCarney {
5398f42947SShawn McCarney   public:
5498f42947SShawn McCarney     // Specify which compiler-generated methods we want
5598f42947SShawn McCarney     DBusInterfacesFinder() = delete;
5698f42947SShawn McCarney     DBusInterfacesFinder(const DBusInterfacesFinder&) = delete;
5798f42947SShawn McCarney     DBusInterfacesFinder(DBusInterfacesFinder&&) = delete;
5898f42947SShawn McCarney     DBusInterfacesFinder& operator=(const DBusInterfacesFinder&) = delete;
5998f42947SShawn McCarney     DBusInterfacesFinder& operator=(DBusInterfacesFinder&&) = delete;
6098f42947SShawn McCarney     ~DBusInterfacesFinder() = default;
6198f42947SShawn McCarney 
6298f42947SShawn McCarney     /**
6398f42947SShawn McCarney      * Callback function that is called when an interface instance is found.
6498f42947SShawn McCarney      *
6598f42947SShawn McCarney      * @param path D-Bus object path that implements the interface
6698f42947SShawn McCarney      * @param interface D-Bus interface that was found
6798f42947SShawn McCarney      * @param properties Properties of the D-Bus interface
6898f42947SShawn McCarney      */
6998f42947SShawn McCarney     using Callback = std::function<void(const std::string& path,
7098f42947SShawn McCarney                                         const std::string& interface,
7198f42947SShawn McCarney                                         const DbusPropertyMap& properties)>;
7298f42947SShawn McCarney 
7398f42947SShawn McCarney     /**
7498f42947SShawn McCarney      * Constructor.
7598f42947SShawn 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      *
8298f42947SShawn McCarney      * @param bus D-Bus bus object
8398f42947SShawn McCarney      * @param service D-Bus service that owns the object paths implementing
8498f42947SShawn McCarney      *                the specified interfaces
8598f42947SShawn McCarney      * @param interfaces D-Bus interfaces to find
8698f42947SShawn McCarney      * @param callback Callback function that is called each time an interface
8798f42947SShawn McCarney      *                 instance is found
8898f42947SShawn McCarney      */
8998f42947SShawn McCarney     explicit DBusInterfacesFinder(sdbusplus::bus_t& bus,
9098f42947SShawn McCarney                                   const std::string& service,
9198f42947SShawn McCarney                                   const std::vector<std::string>& interfaces,
9298f42947SShawn McCarney                                   Callback callback);
9398f42947SShawn McCarney 
9498f42947SShawn McCarney     /**
9598f42947SShawn McCarney      * Callback function to handle InterfacesAdded D-Bus signals
9698f42947SShawn McCarney      *
9798f42947SShawn McCarney      * @param message Expanded sdbusplus message data
9898f42947SShawn McCarney      */
9998f42947SShawn McCarney     void interfacesAddedCallback(sdbusplus::message_t& message);
10098f42947SShawn McCarney 
10198f42947SShawn McCarney   private:
10298f42947SShawn McCarney     /**
10398f42947SShawn McCarney      * Finds any interface instances that already exist on D-Bus.
10498f42947SShawn McCarney      */
10598f42947SShawn McCarney     void findInterfaces();
10698f42947SShawn McCarney 
10798f42947SShawn McCarney     /**
10898f42947SShawn McCarney      * D-Bus bus object.
10998f42947SShawn McCarney      */
11098f42947SShawn McCarney     sdbusplus::bus_t& bus;
11198f42947SShawn McCarney 
11298f42947SShawn McCarney     /**
11398f42947SShawn McCarney      * D-Bus service that owns the object paths implementing the interfaces.
11498f42947SShawn McCarney      */
11598f42947SShawn McCarney     std::string service;
11698f42947SShawn McCarney 
11798f42947SShawn McCarney     /**
11898f42947SShawn McCarney      * D-Bus interfaces to find.
11998f42947SShawn McCarney      */
12098f42947SShawn McCarney     std::vector<std::string> interfaces;
12198f42947SShawn McCarney 
12298f42947SShawn McCarney     /**
12398f42947SShawn McCarney      * Callback function that is called each time an interface instance is
12498f42947SShawn McCarney      * found.
12598f42947SShawn McCarney      */
12698f42947SShawn McCarney     Callback callback;
12798f42947SShawn McCarney 
12898f42947SShawn McCarney     /**
12998f42947SShawn McCarney      * Match object for InterfacesAdded signals.
13098f42947SShawn McCarney      */
13198f42947SShawn McCarney     sdbusplus::bus::match_t match;
13298f42947SShawn McCarney };
13398f42947SShawn McCarney 
13498f42947SShawn McCarney } // namespace phosphor::power::util
135