1*98f42947SShawn McCarney /**
2*98f42947SShawn McCarney  * Copyright © 2024 IBM Corporation
3*98f42947SShawn McCarney  *
4*98f42947SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
5*98f42947SShawn McCarney  * you may not use this file except in compliance with the License.
6*98f42947SShawn McCarney  * You may obtain a copy of the License at
7*98f42947SShawn McCarney  *
8*98f42947SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
9*98f42947SShawn McCarney  *
10*98f42947SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
11*98f42947SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
12*98f42947SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*98f42947SShawn McCarney  * See the License for the specific language governing permissions and
14*98f42947SShawn McCarney  * limitations under the License.
15*98f42947SShawn McCarney  */
16*98f42947SShawn McCarney #pragma once
17*98f42947SShawn McCarney 
18*98f42947SShawn McCarney #include "utility.hpp"
19*98f42947SShawn McCarney 
20*98f42947SShawn McCarney #include <sdbusplus/bus.hpp>
21*98f42947SShawn McCarney #include <sdbusplus/bus/match.hpp>
22*98f42947SShawn McCarney 
23*98f42947SShawn McCarney #include <functional>
24*98f42947SShawn McCarney #include <string>
25*98f42947SShawn McCarney #include <vector>
26*98f42947SShawn McCarney 
27*98f42947SShawn McCarney namespace phosphor::power::util
28*98f42947SShawn McCarney {
29*98f42947SShawn McCarney 
30*98f42947SShawn McCarney /**
31*98f42947SShawn McCarney  * @class DBusInterfacesFinder
32*98f42947SShawn McCarney  *
33*98f42947SShawn McCarney  * Class that finds instances of one or more D-Bus interfaces.
34*98f42947SShawn McCarney  *
35*98f42947SShawn McCarney  * A D-Bus service name and one or more D-Bus interfaces are specified in the
36*98f42947SShawn McCarney  * constructor.  The class finds instances of those interfaces that are owned by
37*98f42947SShawn McCarney  * the service.
38*98f42947SShawn McCarney  *
39*98f42947SShawn McCarney  * The class finds the instances using two different methods:
40*98f42947SShawn McCarney  * - Registers an InterfacesAdded listener for the specified service.  Class is
41*98f42947SShawn McCarney  *   notified when a new interface instance is created on D-Bus.
42*98f42947SShawn McCarney  * - Queries the ObjectMapper to find interface instances that already exist.
43*98f42947SShawn McCarney  *
44*98f42947SShawn McCarney  * Utilizing both methods allows this class to be used before, during, or after
45*98f42947SShawn McCarney  * the service has created the interface instances.
46*98f42947SShawn McCarney  *
47*98f42947SShawn McCarney  * When an interface instance is found, the callback function specified in the
48*98f42947SShawn McCarney  * constructor is called.  This function will be called multiple times if
49*98f42947SShawn McCarney  * multiple instances are found.
50*98f42947SShawn McCarney  */
51*98f42947SShawn McCarney class DBusInterfacesFinder
52*98f42947SShawn McCarney {
53*98f42947SShawn McCarney   public:
54*98f42947SShawn McCarney     // Specify which compiler-generated methods we want
55*98f42947SShawn McCarney     DBusInterfacesFinder() = delete;
56*98f42947SShawn McCarney     DBusInterfacesFinder(const DBusInterfacesFinder&) = delete;
57*98f42947SShawn McCarney     DBusInterfacesFinder(DBusInterfacesFinder&&) = delete;
58*98f42947SShawn McCarney     DBusInterfacesFinder& operator=(const DBusInterfacesFinder&) = delete;
59*98f42947SShawn McCarney     DBusInterfacesFinder& operator=(DBusInterfacesFinder&&) = delete;
60*98f42947SShawn McCarney     ~DBusInterfacesFinder() = default;
61*98f42947SShawn McCarney 
62*98f42947SShawn McCarney     /**
63*98f42947SShawn McCarney      * Callback function that is called when an interface instance is found.
64*98f42947SShawn McCarney      *
65*98f42947SShawn McCarney      * @param path D-Bus object path that implements the interface
66*98f42947SShawn McCarney      * @param interface D-Bus interface that was found
67*98f42947SShawn McCarney      * @param properties Properties of the D-Bus interface
68*98f42947SShawn McCarney      */
69*98f42947SShawn McCarney     using Callback = std::function<void(const std::string& path,
70*98f42947SShawn McCarney                                         const std::string& interface,
71*98f42947SShawn McCarney                                         const DbusPropertyMap& properties)>;
72*98f42947SShawn McCarney 
73*98f42947SShawn McCarney     /**
74*98f42947SShawn McCarney      * Constructor.
75*98f42947SShawn McCarney      *
76*98f42947SShawn McCarney      * @param bus D-Bus bus object
77*98f42947SShawn McCarney      * @param service D-Bus service that owns the object paths implementing
78*98f42947SShawn McCarney      *                the specified interfaces
79*98f42947SShawn McCarney      * @param interfaces D-Bus interfaces to find
80*98f42947SShawn McCarney      * @param callback Callback function that is called each time an interface
81*98f42947SShawn McCarney      *                 instance is found
82*98f42947SShawn McCarney      */
83*98f42947SShawn McCarney     explicit DBusInterfacesFinder(sdbusplus::bus_t& bus,
84*98f42947SShawn McCarney                                   const std::string& service,
85*98f42947SShawn McCarney                                   const std::vector<std::string>& interfaces,
86*98f42947SShawn McCarney                                   Callback callback);
87*98f42947SShawn McCarney 
88*98f42947SShawn McCarney     /**
89*98f42947SShawn McCarney      * Callback function to handle InterfacesAdded D-Bus signals
90*98f42947SShawn McCarney      *
91*98f42947SShawn McCarney      * @param message Expanded sdbusplus message data
92*98f42947SShawn McCarney      */
93*98f42947SShawn McCarney     void interfacesAddedCallback(sdbusplus::message_t& message);
94*98f42947SShawn McCarney 
95*98f42947SShawn McCarney   private:
96*98f42947SShawn McCarney     /**
97*98f42947SShawn McCarney      * Finds any interface instances that already exist on D-Bus.
98*98f42947SShawn McCarney      */
99*98f42947SShawn McCarney     void findInterfaces();
100*98f42947SShawn McCarney 
101*98f42947SShawn McCarney     /**
102*98f42947SShawn McCarney      * D-Bus bus object.
103*98f42947SShawn McCarney      */
104*98f42947SShawn McCarney     sdbusplus::bus_t& bus;
105*98f42947SShawn McCarney 
106*98f42947SShawn McCarney     /**
107*98f42947SShawn McCarney      * D-Bus service that owns the object paths implementing the interfaces.
108*98f42947SShawn McCarney      */
109*98f42947SShawn McCarney     std::string service;
110*98f42947SShawn McCarney 
111*98f42947SShawn McCarney     /**
112*98f42947SShawn McCarney      * D-Bus interfaces to find.
113*98f42947SShawn McCarney      */
114*98f42947SShawn McCarney     std::vector<std::string> interfaces;
115*98f42947SShawn McCarney 
116*98f42947SShawn McCarney     /**
117*98f42947SShawn McCarney      * Callback function that is called each time an interface instance is
118*98f42947SShawn McCarney      * found.
119*98f42947SShawn McCarney      */
120*98f42947SShawn McCarney     Callback callback;
121*98f42947SShawn McCarney 
122*98f42947SShawn McCarney     /**
123*98f42947SShawn McCarney      * Match object for InterfacesAdded signals.
124*98f42947SShawn McCarney      */
125*98f42947SShawn McCarney     sdbusplus::bus::match_t match;
126*98f42947SShawn McCarney };
127*98f42947SShawn McCarney 
128*98f42947SShawn McCarney } // namespace phosphor::power::util
129