xref: /openbmc/phosphor-power/tools/power-utils/utils.hpp (revision 92261f88729b618b1c31d20f28328a8aff73b83b)
114572cf4SShawn McCarney /**
214572cf4SShawn McCarney  * Copyright © 2024 IBM Corporation
314572cf4SShawn McCarney  *
414572cf4SShawn McCarney  * Licensed under the Apache License, Version 2.0 (the "License");
514572cf4SShawn McCarney  * you may not use this file except in compliance with the License.
614572cf4SShawn McCarney  * You may obtain a copy of the License at
714572cf4SShawn McCarney  *
814572cf4SShawn McCarney  *     http://www.apache.org/licenses/LICENSE-2.0
914572cf4SShawn McCarney  *
1014572cf4SShawn McCarney  * Unless required by applicable law or agreed to in writing, software
1114572cf4SShawn McCarney  * distributed under the License is distributed on an "AS IS" BASIS,
1214572cf4SShawn McCarney  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1314572cf4SShawn McCarney  * See the License for the specific language governing permissions and
1414572cf4SShawn McCarney  * limitations under the License.
1514572cf4SShawn McCarney  */
1614572cf4SShawn McCarney #pragma once
1714572cf4SShawn McCarney 
1814572cf4SShawn McCarney #include "pmbus.hpp"
1914572cf4SShawn McCarney 
2014572cf4SShawn McCarney #include <sdbusplus/bus.hpp>
2114572cf4SShawn McCarney 
2214572cf4SShawn McCarney #include <cstdint>
2314572cf4SShawn McCarney #include <memory>
2414572cf4SShawn McCarney #include <string>
2514572cf4SShawn McCarney #include <tuple>
2623dee383SShawn McCarney #include <utility> // for std::pair
2714572cf4SShawn McCarney 
2814572cf4SShawn McCarney /**
2914572cf4SShawn McCarney  * @namespace utils
3014572cf4SShawn McCarney  *
3114572cf4SShawn McCarney  * Contains utility functions used within the psutils tool.
3214572cf4SShawn McCarney  */
3314572cf4SShawn McCarney namespace utils
3414572cf4SShawn McCarney {
3514572cf4SShawn McCarney 
3614572cf4SShawn McCarney // PsuI2cInfo contains the device i2c bus and i2c address
3714572cf4SShawn McCarney using PsuI2cInfo = std::tuple<std::uint64_t, std::uint64_t>;
3814572cf4SShawn McCarney 
3914572cf4SShawn McCarney /**
4014572cf4SShawn McCarney  * @brief Get i2c bus and address
4114572cf4SShawn McCarney  *
4214572cf4SShawn McCarney  * @param[in] bus - Systemd bus connection
4314572cf4SShawn McCarney  * @param[in] psuInventoryPath - The PSU inventory path.
4414572cf4SShawn McCarney  *
4514572cf4SShawn McCarney  * @return tuple - i2cBus and i2cAddr.
4614572cf4SShawn McCarney  */
4714572cf4SShawn McCarney PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus,
4814572cf4SShawn McCarney                      const std::string& psuInventoryPath);
4914572cf4SShawn McCarney 
5014572cf4SShawn McCarney /**
5114572cf4SShawn McCarney  * @brief Get PMBus interface pointer
5214572cf4SShawn McCarney  *
5314572cf4SShawn McCarney  * @param[in] i2cBus - PSU i2c bus
5414572cf4SShawn McCarney  * @param[in] i2cAddr - PSU i2c address
5514572cf4SShawn McCarney  *
5614572cf4SShawn McCarney  * @return Pointer to PSU PMBus interface
5714572cf4SShawn McCarney  */
58*92261f88SPatrick Williams std::unique_ptr<phosphor::pmbus::PMBusBase> getPmbusIntf(std::uint64_t i2cBus,
59*92261f88SPatrick Williams                                                          std::uint64_t i2cAddr);
6014572cf4SShawn McCarney 
6114572cf4SShawn McCarney /**
6214572cf4SShawn McCarney  * @brief Reads a VPD value from PMBus, corrects size, and contents.
6314572cf4SShawn McCarney  *
6414572cf4SShawn McCarney  * If the VPD data read is not the passed in size, resize and fill with
6514572cf4SShawn McCarney  * spaces. If the data contains a non-alphanumeric value, replace any of
6614572cf4SShawn McCarney  * those values with spaces.
6714572cf4SShawn McCarney  *
6814572cf4SShawn McCarney  * @param[in] pmbusIntf - PMBus Interface.
6914572cf4SShawn McCarney  * @param[in] vpdName - The name of the sysfs "file" to read data from.
7014572cf4SShawn McCarney  * @param[in] type - The HWMON file type to read from.
7114572cf4SShawn McCarney  * @param[in] vpdSize - The expected size of the data for this VPD/property
7214572cf4SShawn McCarney  *
7314572cf4SShawn McCarney  * @return A string containing the VPD data read, resized if necessary
7414572cf4SShawn McCarney  */
7514572cf4SShawn McCarney std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
7614572cf4SShawn McCarney                          const std::string& vpdName,
7714572cf4SShawn McCarney                          const phosphor::pmbus::Type& type,
7814572cf4SShawn McCarney                          const std::size_t& vpdSize);
7914572cf4SShawn McCarney 
8014572cf4SShawn McCarney /**
8114572cf4SShawn McCarney  * @brief Check for file existence
8214572cf4SShawn McCarney  *
8314572cf4SShawn McCarney  * @param[in] filePath - File path
8414572cf4SShawn McCarney  *
8514572cf4SShawn McCarney  * @return bool
8614572cf4SShawn McCarney  */
8714572cf4SShawn McCarney bool checkFileExists(const std::string& filePath);
8814572cf4SShawn McCarney 
8923dee383SShawn McCarney /**
9023dee383SShawn McCarney  * @brief Get the device name from the device path
9123dee383SShawn McCarney  *
9223dee383SShawn McCarney  * @param[in] devPath - PSU path
9323dee383SShawn McCarney  *
9423dee383SShawn McCarney  * @return device name e.g. 3-0068
9523dee383SShawn McCarney  */
9623dee383SShawn McCarney std::string getDeviceName(std::string devPath);
9723dee383SShawn McCarney 
9823dee383SShawn McCarney /**
9923dee383SShawn McCarney  * @brief Function to get device path using DBus bus and PSU
10023dee383SShawn McCarney  * inventory Path
10123dee383SShawn McCarney  *
10223dee383SShawn McCarney  * @param[in] bus - The sdbusplus DBus bus connection
10323dee383SShawn McCarney  * @param[in] psuInventoryPath - PSU inventory path
10423dee383SShawn McCarney  *
10523dee383SShawn McCarney  * @return device path e.g. /sys/bus/i2c/devices/3-0068
10623dee383SShawn McCarney  */
10723dee383SShawn McCarney std::string getDevicePath(sdbusplus::bus_t& bus,
10823dee383SShawn McCarney                           const std::string& psuInventoryPath);
10923dee383SShawn McCarney 
11023dee383SShawn McCarney /**
11123dee383SShawn McCarney  * @brief Parse the device name to obtain bus and device address
11223dee383SShawn McCarney  *
11323dee383SShawn McCarney  * @param[in] devName - Device name
11423dee383SShawn McCarney  *
11523dee383SShawn McCarney  * @return bus and device address
11623dee383SShawn McCarney  */
11723dee383SShawn McCarney std::pair<uint8_t, uint8_t> parseDeviceName(const std::string& devName);
11823dee383SShawn McCarney 
11923dee383SShawn McCarney /**
12023dee383SShawn McCarney  * @brief Wrapper to check existence of PSU JSON file
12123dee383SShawn McCarney  *
12223dee383SShawn McCarney  * @return true or false (true if using JSON file)
12323dee383SShawn McCarney  */
12423dee383SShawn McCarney bool usePsuJsonFile();
12523dee383SShawn McCarney 
12614572cf4SShawn McCarney } // namespace utils
127