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_t& 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_t& bus) :
115         bus{bus}, errorLogging{bus}, presenceService{bus}, sensors{bus},
116         vpd{bus}
117     {}
118 
119     /** @copydoc Services::getBus() */
120     virtual sdbusplus::bus_t& getBus() override
121     {
122         return bus;
123     }
124 
125     /** @copydoc Services::getErrorLogging() */
126     virtual ErrorLogging& getErrorLogging() override
127     {
128         return errorLogging;
129     }
130 
131     /** @copydoc Services::getJournal() */
132     virtual Journal& getJournal() override
133     {
134         return journal;
135     }
136 
137     /** @copydoc Services::getPresenceService() */
138     virtual PresenceService& getPresenceService() override
139     {
140         return presenceService;
141     }
142 
143     /** @copydoc Services::getSensors() */
144     virtual Sensors& getSensors() override
145     {
146         return sensors;
147     }
148 
149     /** @copydoc Services::getVPD() */
150     virtual VPD& getVPD() override
151     {
152         return vpd;
153     }
154 
155   private:
156     /**
157      * D-Bus bus object.
158      */
159     sdbusplus::bus_t& bus;
160 
161     /**
162      * Implementation of the ErrorLogging interface using D-Bus method calls.
163      */
164     DBusErrorLogging errorLogging;
165 
166     /**
167      * Implementation of the Journal interface that writes to the systemd
168      * journal.
169      */
170     SystemdJournal journal{};
171 
172     /**
173      * Implementation of the PresenceService interface using D-Bus method calls.
174      */
175     DBusPresenceService presenceService;
176 
177     /**
178      * Implementation of the Sensors interface using D-Bus.
179      */
180     DBusSensors sensors;
181 
182     /**
183      * Implementation of the VPD interface using D-Bus method calls.
184      */
185     DBusVPD vpd;
186 };
187 
188 } // namespace phosphor::power::regulators
189