1 /**
2  * Copyright © 2020 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_sensors.hpp"
19 #include "error_logging.hpp"
20 #include "journal.hpp"
21 #include "presence_service.hpp"
22 #include "sensors.hpp"
23 #include "vpd.hpp"
24 
25 #include <sdbusplus/bus.hpp>
26 
27 namespace phosphor::power::regulators
28 {
29 
30 /**
31  * @class Services
32  *
33  * Abstract base class that provides an interface to system services like error
34  * logging and the journal.
35  *
36  * This interface is a container for a set of system services.  It can be passed
37  * as a single parameter to the rest of the application.
38  */
39 class Services
40 {
41   public:
42     // Specify which compiler-generated methods we want
43     Services() = default;
44     Services(const Services&) = delete;
45     Services(Services&&) = delete;
46     Services& operator=(const Services&) = delete;
47     Services& operator=(Services&&) = delete;
48     virtual ~Services() = default;
49 
50     /**
51      * Returns the D-Bus bus object.
52      *
53      * @return D-Bus bus
54      */
55     virtual sdbusplus::bus::bus& getBus() = 0;
56 
57     /**
58      * Returns the error logging interface.
59      *
60      * @return error logging interface
61      */
62     virtual ErrorLogging& getErrorLogging() = 0;
63 
64     /**
65      * Returns the journal interface.
66      *
67      * @return journal interface
68      */
69     virtual Journal& getJournal() = 0;
70 
71     /**
72      * Returns the interface to hardware presence data.
73      *
74      * @return hardware presence interface
75      */
76     virtual PresenceService& getPresenceService() = 0;
77 
78     /**
79      * Returns the sensors interface.
80      *
81      * @return sensors interface
82      */
83     virtual Sensors& getSensors() = 0;
84 
85     /**
86      * Returns the interface to hardware VPD (Vital Product Data).
87      *
88      * @return hardware VPD interface
89      */
90     virtual VPD& getVPD() = 0;
91 };
92 
93 /**
94  * @class BMCServices
95  *
96  * Implementation of the Services interface using standard BMC system services.
97  */
98 class BMCServices : public Services
99 {
100   public:
101     // Specify which compiler-generated methods we want
102     BMCServices() = delete;
103     BMCServices(const BMCServices&) = delete;
104     BMCServices(BMCServices&&) = delete;
105     BMCServices& operator=(const BMCServices&) = delete;
106     BMCServices& operator=(BMCServices&&) = delete;
107     virtual ~BMCServices() = default;
108 
109     /**
110      * Constructor.
111      *
112      * @param bus D-Bus bus object
113      */
114     explicit BMCServices(sdbusplus::bus::bus& bus) :
115         bus{bus}, errorLogging{bus},
116         presenceService{bus}, sensors{bus}, vpd{bus}
117     {
118     }
119 
120     /** @copydoc Services::getBus() */
121     virtual sdbusplus::bus::bus& getBus() override
122     {
123         return bus;
124     }
125 
126     /** @copydoc Services::getErrorLogging() */
127     virtual ErrorLogging& getErrorLogging() override
128     {
129         return errorLogging;
130     }
131 
132     /** @copydoc Services::getJournal() */
133     virtual Journal& getJournal() override
134     {
135         return journal;
136     }
137 
138     /** @copydoc Services::getPresenceService() */
139     virtual PresenceService& getPresenceService() override
140     {
141         return presenceService;
142     }
143 
144     /** @copydoc Services::getSensors() */
145     virtual Sensors& getSensors() override
146     {
147         return sensors;
148     }
149 
150     /** @copydoc Services::getVPD() */
151     virtual VPD& getVPD() override
152     {
153         return vpd;
154     }
155 
156   private:
157     /**
158      * D-Bus bus object.
159      */
160     sdbusplus::bus::bus& bus;
161 
162     /**
163      * Implementation of the ErrorLogging interface using D-Bus method calls.
164      */
165     DBusErrorLogging errorLogging;
166 
167     /**
168      * Implementation of the Journal interface that writes to the systemd
169      * journal.
170      */
171     SystemdJournal journal{};
172 
173     /**
174      * Implementation of the PresenceService interface using D-Bus method calls.
175      */
176     DBusPresenceService presenceService;
177 
178     /**
179      * Implementation of the Sensors interface using D-Bus.
180      */
181     DBusSensors sensors;
182 
183     /**
184      * Implementation of the VPD interface using D-Bus method calls.
185      */
186     DBusVPD vpd;
187 };
188 
189 } // namespace phosphor::power::regulators
190