xref: /openbmc/phosphor-power/phosphor-power-sequencer/src/services.hpp (revision f54021972b91be5058b50e9046bb0dd5a3b22a80)
1906cc3f3SShawn McCarney /**
2906cc3f3SShawn McCarney  * Copyright © 2024 IBM Corporation
3906cc3f3SShawn McCarney  *
4906cc3f3SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
5906cc3f3SShawn McCarney  * you may not use this file except in compliance with the License.
6906cc3f3SShawn McCarney  * You may obtain a copy of the License at
7906cc3f3SShawn McCarney  *
8906cc3f3SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
9906cc3f3SShawn McCarney  *
10906cc3f3SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
11906cc3f3SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
12906cc3f3SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13906cc3f3SShawn McCarney  * See the License for the specific language governing permissions and
14906cc3f3SShawn McCarney  * limitations under the License.
15906cc3f3SShawn McCarney  */
16906cc3f3SShawn McCarney #pragma once
17906cc3f3SShawn McCarney 
18906cc3f3SShawn McCarney #include "pmbus.hpp"
19906cc3f3SShawn McCarney #include "xyz/openbmc_project/Logging/Entry/server.hpp"
20906cc3f3SShawn McCarney 
21906cc3f3SShawn McCarney #include <phosphor-logging/lg2.hpp>
22906cc3f3SShawn McCarney #include <sdbusplus/bus.hpp>
23906cc3f3SShawn McCarney #include <sdbusplus/exception.hpp>
24906cc3f3SShawn McCarney 
25906cc3f3SShawn McCarney #include <cstdint>
26e4fef0fcSShawn McCarney #include <format>
27906cc3f3SShawn McCarney #include <map>
28906cc3f3SShawn McCarney #include <memory>
29906cc3f3SShawn McCarney #include <string>
30906cc3f3SShawn McCarney #include <vector>
31906cc3f3SShawn McCarney 
32906cc3f3SShawn McCarney namespace phosphor::power::sequencer
33906cc3f3SShawn McCarney {
34906cc3f3SShawn McCarney 
35906cc3f3SShawn McCarney using namespace sdbusplus::xyz::openbmc_project::Logging::server;
36906cc3f3SShawn McCarney using PMBusBase = phosphor::pmbus::PMBusBase;
37906cc3f3SShawn McCarney using PMBus = phosphor::pmbus::PMBus;
38906cc3f3SShawn McCarney 
39906cc3f3SShawn McCarney /**
40906cc3f3SShawn McCarney  * @class Services
41906cc3f3SShawn McCarney  *
42906cc3f3SShawn McCarney  * Abstract base class that provides an interface to system services like error
43906cc3f3SShawn McCarney  * logging and the journal.
44906cc3f3SShawn McCarney  */
45906cc3f3SShawn McCarney class Services
46906cc3f3SShawn McCarney {
47906cc3f3SShawn McCarney   public:
48906cc3f3SShawn McCarney     // Specify which compiler-generated methods we want
49906cc3f3SShawn McCarney     Services() = default;
50906cc3f3SShawn McCarney     Services(const Services&) = delete;
51906cc3f3SShawn McCarney     Services(Services&&) = delete;
52906cc3f3SShawn McCarney     Services& operator=(const Services&) = delete;
53906cc3f3SShawn McCarney     Services& operator=(Services&&) = delete;
54906cc3f3SShawn McCarney     virtual ~Services() = default;
55906cc3f3SShawn McCarney 
56906cc3f3SShawn McCarney     /**
57906cc3f3SShawn McCarney      * Returns the D-Bus bus object.
58906cc3f3SShawn McCarney      *
59906cc3f3SShawn McCarney      * @return D-Bus bus
60906cc3f3SShawn McCarney      */
61906cc3f3SShawn McCarney     virtual sdbusplus::bus_t& getBus() = 0;
62906cc3f3SShawn McCarney 
63906cc3f3SShawn McCarney     /**
64906cc3f3SShawn McCarney      * Logs an error message in the system journal.
65906cc3f3SShawn McCarney      *
66906cc3f3SShawn McCarney      * @param message message to log
67906cc3f3SShawn McCarney      */
68906cc3f3SShawn McCarney     virtual void logErrorMsg(const std::string& message) = 0;
69906cc3f3SShawn McCarney 
70906cc3f3SShawn McCarney     /**
71906cc3f3SShawn McCarney      * Logs an informational message in the system journal.
72906cc3f3SShawn McCarney      *
73906cc3f3SShawn McCarney      * @param message message to log
74906cc3f3SShawn McCarney      */
75906cc3f3SShawn McCarney     virtual void logInfoMsg(const std::string& message) = 0;
76906cc3f3SShawn McCarney 
77906cc3f3SShawn McCarney     /**
78906cc3f3SShawn McCarney      * Logs an error.
79906cc3f3SShawn McCarney      *
80906cc3f3SShawn McCarney      * If logging fails, a message is written to the system journal but an
81906cc3f3SShawn McCarney      * exception is not thrown.
82906cc3f3SShawn McCarney      *
83906cc3f3SShawn McCarney      * @param message Message property of the error log entry
84906cc3f3SShawn McCarney      * @param severity Severity property of the error log entry
85906cc3f3SShawn McCarney      * @param additionalData AdditionalData property of the error log entry
86906cc3f3SShawn McCarney      */
87906cc3f3SShawn McCarney     virtual void
88906cc3f3SShawn McCarney         logError(const std::string& message, Entry::Level severity,
89906cc3f3SShawn McCarney                  std::map<std::string, std::string>& additionalData) = 0;
90906cc3f3SShawn McCarney 
91906cc3f3SShawn McCarney     /**
92906cc3f3SShawn McCarney      * Returns whether the hardware with the specified inventory path is
93906cc3f3SShawn McCarney      * present.
94906cc3f3SShawn McCarney      *
95906cc3f3SShawn McCarney      * Throws an exception if an error occurs while obtaining the presence
96906cc3f3SShawn McCarney      * value.
97906cc3f3SShawn McCarney      *
98906cc3f3SShawn McCarney      * @param inventoryPath D-Bus inventory path of the hardware
99906cc3f3SShawn McCarney      * @return true if hardware is present, false otherwise
100906cc3f3SShawn McCarney      */
101906cc3f3SShawn McCarney     virtual bool isPresent(const std::string& inventoryPath) = 0;
102906cc3f3SShawn McCarney 
103906cc3f3SShawn McCarney     /**
104906cc3f3SShawn McCarney      * Reads all the GPIO values on the chip with the specified label.
105906cc3f3SShawn McCarney      *
106906cc3f3SShawn McCarney      * Throws an exception if an error occurs while obtaining the values.
107906cc3f3SShawn McCarney      *
108906cc3f3SShawn McCarney      * @param chipLabel label identifying the chip with the GPIOs
109906cc3f3SShawn McCarney      * @return GPIO values
110906cc3f3SShawn McCarney      */
111906cc3f3SShawn McCarney     virtual std::vector<int> getGPIOValues(const std::string& chipLabel) = 0;
112906cc3f3SShawn McCarney 
113906cc3f3SShawn McCarney     /**
114906cc3f3SShawn McCarney      * Creates object for communicating with a PMBus device by reading and
115906cc3f3SShawn McCarney      * writing sysfs files.
116906cc3f3SShawn McCarney      *
117906cc3f3SShawn McCarney      * Throws an exception if an error occurs.
118906cc3f3SShawn McCarney      *
119906cc3f3SShawn McCarney      * @param bus I2C bus
120906cc3f3SShawn McCarney      * @param address I2C address
121906cc3f3SShawn McCarney      * @param driverName Device driver name
122906cc3f3SShawn McCarney      * @param instance Chip instance number
123906cc3f3SShawn McCarney      * @return object for communicating with PMBus device
124906cc3f3SShawn McCarney      */
125*f5402197SPatrick Williams     virtual std::unique_ptr<PMBusBase> createPMBus(
126*f5402197SPatrick Williams         uint8_t bus, uint16_t address, const std::string& driverName = "",
127906cc3f3SShawn McCarney         size_t instance = 0) = 0;
128e4fef0fcSShawn McCarney 
129e4fef0fcSShawn McCarney     /**
1303a11d632SShawn McCarney      * Creates a BMC dump.
1313a11d632SShawn McCarney      */
1323a11d632SShawn McCarney     virtual void createBMCDump() = 0;
1333a11d632SShawn McCarney 
1343a11d632SShawn McCarney     /**
135e4fef0fcSShawn McCarney      * Clear any cached data.
136e4fef0fcSShawn McCarney      *
137e4fef0fcSShawn McCarney      * Some data may be cached for performance reasons, such as hardware
138e4fef0fcSShawn McCarney      * presence.  Clearing the cache results in the latest data being obtained
139e4fef0fcSShawn McCarney      * by a subsequent method calls.
140e4fef0fcSShawn McCarney      */
141e4fef0fcSShawn McCarney     virtual void clearCache() = 0;
142906cc3f3SShawn McCarney };
143906cc3f3SShawn McCarney 
144906cc3f3SShawn McCarney /**
145906cc3f3SShawn McCarney  * @class BMCServices
146906cc3f3SShawn McCarney  *
147906cc3f3SShawn McCarney  * Implementation of the Services interface using standard BMC system services.
148906cc3f3SShawn McCarney  */
149906cc3f3SShawn McCarney class BMCServices : public Services
150906cc3f3SShawn McCarney {
151906cc3f3SShawn McCarney   public:
152906cc3f3SShawn McCarney     // Specify which compiler-generated methods we want
153906cc3f3SShawn McCarney     BMCServices() = delete;
154906cc3f3SShawn McCarney     BMCServices(const BMCServices&) = delete;
155906cc3f3SShawn McCarney     BMCServices(BMCServices&&) = delete;
156906cc3f3SShawn McCarney     BMCServices& operator=(const BMCServices&) = delete;
157906cc3f3SShawn McCarney     BMCServices& operator=(BMCServices&&) = delete;
158906cc3f3SShawn McCarney     virtual ~BMCServices() = default;
159906cc3f3SShawn McCarney 
160906cc3f3SShawn McCarney     /**
161906cc3f3SShawn McCarney      * Constructor.
162906cc3f3SShawn McCarney      *
163906cc3f3SShawn McCarney      * @param bus D-Bus bus object
164906cc3f3SShawn McCarney      */
BMCServices(sdbusplus::bus_t & bus)165906cc3f3SShawn McCarney     explicit BMCServices(sdbusplus::bus_t& bus) : bus{bus} {}
166906cc3f3SShawn McCarney 
167906cc3f3SShawn McCarney     /** @copydoc Services::getBus() */
getBus()168906cc3f3SShawn McCarney     virtual sdbusplus::bus_t& getBus() override
169906cc3f3SShawn McCarney     {
170906cc3f3SShawn McCarney         return bus;
171906cc3f3SShawn McCarney     }
172906cc3f3SShawn McCarney 
173906cc3f3SShawn McCarney     /** @copydoc Services::logErrorMsg() */
logErrorMsg(const std::string & message)174906cc3f3SShawn McCarney     virtual void logErrorMsg(const std::string& message) override
175906cc3f3SShawn McCarney     {
176906cc3f3SShawn McCarney         lg2::error(message.c_str());
177906cc3f3SShawn McCarney     }
178906cc3f3SShawn McCarney 
179906cc3f3SShawn McCarney     /** @copydoc Services::logInfoMsg() */
logInfoMsg(const std::string & message)180906cc3f3SShawn McCarney     virtual void logInfoMsg(const std::string& message) override
181906cc3f3SShawn McCarney     {
182906cc3f3SShawn McCarney         lg2::info(message.c_str());
183906cc3f3SShawn McCarney     }
184906cc3f3SShawn McCarney 
185906cc3f3SShawn McCarney     /** @copydoc Services::logError() */
186906cc3f3SShawn McCarney     virtual void
187906cc3f3SShawn McCarney         logError(const std::string& message, Entry::Level severity,
188906cc3f3SShawn McCarney                  std::map<std::string, std::string>& additionalData) override;
189906cc3f3SShawn McCarney 
190906cc3f3SShawn McCarney     /** @copydoc Services::isPresent() */
191906cc3f3SShawn McCarney     virtual bool isPresent(const std::string& inventoryPath) override;
192906cc3f3SShawn McCarney 
193906cc3f3SShawn McCarney     /** @copydoc Services::getGPIOValues() */
194906cc3f3SShawn McCarney     virtual std::vector<int>
195906cc3f3SShawn McCarney         getGPIOValues(const std::string& chipLabel) override;
196906cc3f3SShawn McCarney 
197906cc3f3SShawn McCarney     /** @copydoc Services::createPMBus() */
createPMBus(uint8_t bus,uint16_t address,const std::string & driverName="",size_t instance=0)198*f5402197SPatrick Williams     virtual std::unique_ptr<PMBusBase> createPMBus(
199*f5402197SPatrick Williams         uint8_t bus, uint16_t address, const std::string& driverName = "",
200906cc3f3SShawn McCarney         size_t instance = 0) override
201906cc3f3SShawn McCarney     {
202*f5402197SPatrick Williams         std::string path =
203*f5402197SPatrick Williams             std::format("/sys/bus/i2c/devices/{}-{:04x}", bus, address);
204906cc3f3SShawn McCarney         return std::make_unique<PMBus>(path, driverName, instance);
205906cc3f3SShawn McCarney     }
206906cc3f3SShawn McCarney 
2073a11d632SShawn McCarney     /** @copydoc Services::createBMCDump() */
2083a11d632SShawn McCarney     virtual void createBMCDump() override;
2093a11d632SShawn McCarney 
210e4fef0fcSShawn McCarney     /** @copydoc Services::clearCache() */
clearCache()211e4fef0fcSShawn McCarney     virtual void clearCache() override
212e4fef0fcSShawn McCarney     {
213e4fef0fcSShawn McCarney         presenceCache.clear();
214e4fef0fcSShawn McCarney     }
215e4fef0fcSShawn McCarney 
216906cc3f3SShawn McCarney   private:
217906cc3f3SShawn McCarney     /**
218906cc3f3SShawn McCarney      * Returns whether the specified D-Bus exception is one of the expected
219906cc3f3SShawn McCarney      * types that can be thrown if hardware is not present.
220906cc3f3SShawn McCarney      *
221906cc3f3SShawn McCarney      * @return true if exception type is expected, false otherwise
222906cc3f3SShawn McCarney      */
223906cc3f3SShawn McCarney     bool isExpectedException(const sdbusplus::exception_t& e);
224906cc3f3SShawn McCarney 
225906cc3f3SShawn McCarney     /**
226906cc3f3SShawn McCarney      * D-Bus bus object.
227906cc3f3SShawn McCarney      */
228906cc3f3SShawn McCarney     sdbusplus::bus_t& bus;
229e4fef0fcSShawn McCarney 
230e4fef0fcSShawn McCarney     /**
231e4fef0fcSShawn McCarney      * Cached presence data.
232e4fef0fcSShawn McCarney      *
233e4fef0fcSShawn McCarney      * Map from inventory paths to presence values.
234e4fef0fcSShawn McCarney      */
235e4fef0fcSShawn McCarney     std::map<std::string, bool> presenceCache{};
236906cc3f3SShawn McCarney };
237906cc3f3SShawn McCarney 
238906cc3f3SShawn McCarney } // namespace phosphor::power::sequencer
239