1*906cc3f3SShawn McCarney /** 2*906cc3f3SShawn McCarney * Copyright © 2024 IBM Corporation 3*906cc3f3SShawn McCarney * 4*906cc3f3SShawn McCarney * Licensed under the Apache License, Version 2.0 (the "License"); 5*906cc3f3SShawn McCarney * you may not use this file except in compliance with the License. 6*906cc3f3SShawn McCarney * You may obtain a copy of the License at 7*906cc3f3SShawn McCarney * 8*906cc3f3SShawn McCarney * http://www.apache.org/licenses/LICENSE-2.0 9*906cc3f3SShawn McCarney * 10*906cc3f3SShawn McCarney * Unless required by applicable law or agreed to in writing, software 11*906cc3f3SShawn McCarney * distributed under the License is distributed on an "AS IS" BASIS, 12*906cc3f3SShawn McCarney * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*906cc3f3SShawn McCarney * See the License for the specific language governing permissions and 14*906cc3f3SShawn McCarney * limitations under the License. 15*906cc3f3SShawn McCarney */ 16*906cc3f3SShawn McCarney #pragma once 17*906cc3f3SShawn McCarney 18*906cc3f3SShawn McCarney #include "pmbus.hpp" 19*906cc3f3SShawn McCarney #include "xyz/openbmc_project/Logging/Entry/server.hpp" 20*906cc3f3SShawn McCarney 21*906cc3f3SShawn McCarney #include <fmt/format.h> 22*906cc3f3SShawn McCarney 23*906cc3f3SShawn McCarney #include <phosphor-logging/lg2.hpp> 24*906cc3f3SShawn McCarney #include <sdbusplus/bus.hpp> 25*906cc3f3SShawn McCarney #include <sdbusplus/exception.hpp> 26*906cc3f3SShawn McCarney 27*906cc3f3SShawn McCarney #include <cstdint> 28*906cc3f3SShawn McCarney #include <map> 29*906cc3f3SShawn McCarney #include <memory> 30*906cc3f3SShawn McCarney #include <string> 31*906cc3f3SShawn McCarney #include <vector> 32*906cc3f3SShawn McCarney 33*906cc3f3SShawn McCarney namespace phosphor::power::sequencer 34*906cc3f3SShawn McCarney { 35*906cc3f3SShawn McCarney 36*906cc3f3SShawn McCarney using namespace sdbusplus::xyz::openbmc_project::Logging::server; 37*906cc3f3SShawn McCarney using PMBusBase = phosphor::pmbus::PMBusBase; 38*906cc3f3SShawn McCarney using PMBus = phosphor::pmbus::PMBus; 39*906cc3f3SShawn McCarney 40*906cc3f3SShawn McCarney /** 41*906cc3f3SShawn McCarney * @class Services 42*906cc3f3SShawn McCarney * 43*906cc3f3SShawn McCarney * Abstract base class that provides an interface to system services like error 44*906cc3f3SShawn McCarney * logging and the journal. 45*906cc3f3SShawn McCarney */ 46*906cc3f3SShawn McCarney class Services 47*906cc3f3SShawn McCarney { 48*906cc3f3SShawn McCarney public: 49*906cc3f3SShawn McCarney // Specify which compiler-generated methods we want 50*906cc3f3SShawn McCarney Services() = default; 51*906cc3f3SShawn McCarney Services(const Services&) = delete; 52*906cc3f3SShawn McCarney Services(Services&&) = delete; 53*906cc3f3SShawn McCarney Services& operator=(const Services&) = delete; 54*906cc3f3SShawn McCarney Services& operator=(Services&&) = delete; 55*906cc3f3SShawn McCarney virtual ~Services() = default; 56*906cc3f3SShawn McCarney 57*906cc3f3SShawn McCarney /** 58*906cc3f3SShawn McCarney * Returns the D-Bus bus object. 59*906cc3f3SShawn McCarney * 60*906cc3f3SShawn McCarney * @return D-Bus bus 61*906cc3f3SShawn McCarney */ 62*906cc3f3SShawn McCarney virtual sdbusplus::bus_t& getBus() = 0; 63*906cc3f3SShawn McCarney 64*906cc3f3SShawn McCarney /** 65*906cc3f3SShawn McCarney * Logs an error message in the system journal. 66*906cc3f3SShawn McCarney * 67*906cc3f3SShawn McCarney * @param message message to log 68*906cc3f3SShawn McCarney */ 69*906cc3f3SShawn McCarney virtual void logErrorMsg(const std::string& message) = 0; 70*906cc3f3SShawn McCarney 71*906cc3f3SShawn McCarney /** 72*906cc3f3SShawn McCarney * Logs an informational message in the system journal. 73*906cc3f3SShawn McCarney * 74*906cc3f3SShawn McCarney * @param message message to log 75*906cc3f3SShawn McCarney */ 76*906cc3f3SShawn McCarney virtual void logInfoMsg(const std::string& message) = 0; 77*906cc3f3SShawn McCarney 78*906cc3f3SShawn McCarney /** 79*906cc3f3SShawn McCarney * Logs an error. 80*906cc3f3SShawn McCarney * 81*906cc3f3SShawn McCarney * If logging fails, a message is written to the system journal but an 82*906cc3f3SShawn McCarney * exception is not thrown. 83*906cc3f3SShawn McCarney * 84*906cc3f3SShawn McCarney * @param message Message property of the error log entry 85*906cc3f3SShawn McCarney * @param severity Severity property of the error log entry 86*906cc3f3SShawn McCarney * @param additionalData AdditionalData property of the error log entry 87*906cc3f3SShawn McCarney */ 88*906cc3f3SShawn McCarney virtual void 89*906cc3f3SShawn McCarney logError(const std::string& message, Entry::Level severity, 90*906cc3f3SShawn McCarney std::map<std::string, std::string>& additionalData) = 0; 91*906cc3f3SShawn McCarney 92*906cc3f3SShawn McCarney /** 93*906cc3f3SShawn McCarney * Returns whether the hardware with the specified inventory path is 94*906cc3f3SShawn McCarney * present. 95*906cc3f3SShawn McCarney * 96*906cc3f3SShawn McCarney * Throws an exception if an error occurs while obtaining the presence 97*906cc3f3SShawn McCarney * value. 98*906cc3f3SShawn McCarney * 99*906cc3f3SShawn McCarney * @param inventoryPath D-Bus inventory path of the hardware 100*906cc3f3SShawn McCarney * @return true if hardware is present, false otherwise 101*906cc3f3SShawn McCarney */ 102*906cc3f3SShawn McCarney virtual bool isPresent(const std::string& inventoryPath) = 0; 103*906cc3f3SShawn McCarney 104*906cc3f3SShawn McCarney /** 105*906cc3f3SShawn McCarney * Reads all the GPIO values on the chip with the specified label. 106*906cc3f3SShawn McCarney * 107*906cc3f3SShawn McCarney * Throws an exception if an error occurs while obtaining the values. 108*906cc3f3SShawn McCarney * 109*906cc3f3SShawn McCarney * @param chipLabel label identifying the chip with the GPIOs 110*906cc3f3SShawn McCarney * @return GPIO values 111*906cc3f3SShawn McCarney */ 112*906cc3f3SShawn McCarney virtual std::vector<int> getGPIOValues(const std::string& chipLabel) = 0; 113*906cc3f3SShawn McCarney 114*906cc3f3SShawn McCarney /** 115*906cc3f3SShawn McCarney * Creates object for communicating with a PMBus device by reading and 116*906cc3f3SShawn McCarney * writing sysfs files. 117*906cc3f3SShawn McCarney * 118*906cc3f3SShawn McCarney * Throws an exception if an error occurs. 119*906cc3f3SShawn McCarney * 120*906cc3f3SShawn McCarney * @param bus I2C bus 121*906cc3f3SShawn McCarney * @param address I2C address 122*906cc3f3SShawn McCarney * @param driverName Device driver name 123*906cc3f3SShawn McCarney * @param instance Chip instance number 124*906cc3f3SShawn McCarney * @return object for communicating with PMBus device 125*906cc3f3SShawn McCarney */ 126*906cc3f3SShawn McCarney virtual std::unique_ptr<PMBusBase> 127*906cc3f3SShawn McCarney createPMBus(uint8_t bus, uint16_t address, 128*906cc3f3SShawn McCarney const std::string& driverName = "", 129*906cc3f3SShawn McCarney size_t instance = 0) = 0; 130*906cc3f3SShawn McCarney }; 131*906cc3f3SShawn McCarney 132*906cc3f3SShawn McCarney /** 133*906cc3f3SShawn McCarney * @class BMCServices 134*906cc3f3SShawn McCarney * 135*906cc3f3SShawn McCarney * Implementation of the Services interface using standard BMC system services. 136*906cc3f3SShawn McCarney */ 137*906cc3f3SShawn McCarney class BMCServices : public Services 138*906cc3f3SShawn McCarney { 139*906cc3f3SShawn McCarney public: 140*906cc3f3SShawn McCarney // Specify which compiler-generated methods we want 141*906cc3f3SShawn McCarney BMCServices() = delete; 142*906cc3f3SShawn McCarney BMCServices(const BMCServices&) = delete; 143*906cc3f3SShawn McCarney BMCServices(BMCServices&&) = delete; 144*906cc3f3SShawn McCarney BMCServices& operator=(const BMCServices&) = delete; 145*906cc3f3SShawn McCarney BMCServices& operator=(BMCServices&&) = delete; 146*906cc3f3SShawn McCarney virtual ~BMCServices() = default; 147*906cc3f3SShawn McCarney 148*906cc3f3SShawn McCarney /** 149*906cc3f3SShawn McCarney * Constructor. 150*906cc3f3SShawn McCarney * 151*906cc3f3SShawn McCarney * @param bus D-Bus bus object 152*906cc3f3SShawn McCarney */ 153*906cc3f3SShawn McCarney explicit BMCServices(sdbusplus::bus_t& bus) : bus{bus} {} 154*906cc3f3SShawn McCarney 155*906cc3f3SShawn McCarney /** @copydoc Services::getBus() */ 156*906cc3f3SShawn McCarney virtual sdbusplus::bus_t& getBus() override 157*906cc3f3SShawn McCarney { 158*906cc3f3SShawn McCarney return bus; 159*906cc3f3SShawn McCarney } 160*906cc3f3SShawn McCarney 161*906cc3f3SShawn McCarney /** @copydoc Services::logErrorMsg() */ 162*906cc3f3SShawn McCarney virtual void logErrorMsg(const std::string& message) override 163*906cc3f3SShawn McCarney { 164*906cc3f3SShawn McCarney lg2::error(message.c_str()); 165*906cc3f3SShawn McCarney } 166*906cc3f3SShawn McCarney 167*906cc3f3SShawn McCarney /** @copydoc Services::logInfoMsg() */ 168*906cc3f3SShawn McCarney virtual void logInfoMsg(const std::string& message) override 169*906cc3f3SShawn McCarney { 170*906cc3f3SShawn McCarney lg2::info(message.c_str()); 171*906cc3f3SShawn McCarney } 172*906cc3f3SShawn McCarney 173*906cc3f3SShawn McCarney /** @copydoc Services::logError() */ 174*906cc3f3SShawn McCarney virtual void 175*906cc3f3SShawn McCarney logError(const std::string& message, Entry::Level severity, 176*906cc3f3SShawn McCarney std::map<std::string, std::string>& additionalData) override; 177*906cc3f3SShawn McCarney 178*906cc3f3SShawn McCarney /** @copydoc Services::isPresent() */ 179*906cc3f3SShawn McCarney virtual bool isPresent(const std::string& inventoryPath) override; 180*906cc3f3SShawn McCarney 181*906cc3f3SShawn McCarney /** @copydoc Services::getGPIOValues() */ 182*906cc3f3SShawn McCarney virtual std::vector<int> 183*906cc3f3SShawn McCarney getGPIOValues(const std::string& chipLabel) override; 184*906cc3f3SShawn McCarney 185*906cc3f3SShawn McCarney /** @copydoc Services::createPMBus() */ 186*906cc3f3SShawn McCarney virtual std::unique_ptr<PMBusBase> 187*906cc3f3SShawn McCarney createPMBus(uint8_t bus, uint16_t address, 188*906cc3f3SShawn McCarney const std::string& driverName = "", 189*906cc3f3SShawn McCarney size_t instance = 0) override 190*906cc3f3SShawn McCarney { 191*906cc3f3SShawn McCarney std::string path = fmt::format("/sys/bus/i2c/devices/{}-{:04x}", bus, 192*906cc3f3SShawn McCarney address); 193*906cc3f3SShawn McCarney return std::make_unique<PMBus>(path, driverName, instance); 194*906cc3f3SShawn McCarney } 195*906cc3f3SShawn McCarney 196*906cc3f3SShawn McCarney private: 197*906cc3f3SShawn McCarney /** 198*906cc3f3SShawn McCarney * Returns whether the specified D-Bus exception is one of the expected 199*906cc3f3SShawn McCarney * types that can be thrown if hardware is not present. 200*906cc3f3SShawn McCarney * 201*906cc3f3SShawn McCarney * @return true if exception type is expected, false otherwise 202*906cc3f3SShawn McCarney */ 203*906cc3f3SShawn McCarney bool isExpectedException(const sdbusplus::exception_t& e); 204*906cc3f3SShawn McCarney 205*906cc3f3SShawn McCarney /** 206*906cc3f3SShawn McCarney * D-Bus bus object. 207*906cc3f3SShawn McCarney */ 208*906cc3f3SShawn McCarney sdbusplus::bus_t& bus; 209*906cc3f3SShawn McCarney }; 210*906cc3f3SShawn McCarney 211*906cc3f3SShawn McCarney } // namespace phosphor::power::sequencer 212