xref: /openbmc/phosphor-power/tools/power-utils/utils.hpp (revision 23dee383ca3a45889fbb4f7bbae65fe3325c5805)
1 /**
2  * Copyright © 2024 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 "pmbus.hpp"
19 
20 #include <sdbusplus/bus.hpp>
21 
22 #include <cstdint>
23 #include <memory>
24 #include <string>
25 #include <tuple>
26 #include <utility> // for std::pair
27 
28 /**
29  * @namespace utils
30  *
31  * Contains utility functions used within the psutils tool.
32  */
33 namespace utils
34 {
35 
36 // PsuI2cInfo contains the device i2c bus and i2c address
37 using PsuI2cInfo = std::tuple<std::uint64_t, std::uint64_t>;
38 
39 /**
40  * @brief Get i2c bus and address
41  *
42  * @param[in] bus - Systemd bus connection
43  * @param[in] psuInventoryPath - The PSU inventory path.
44  *
45  * @return tuple - i2cBus and i2cAddr.
46  */
47 PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus,
48                      const std::string& psuInventoryPath);
49 
50 /**
51  * @brief Get PMBus interface pointer
52  *
53  * @param[in] i2cBus - PSU i2c bus
54  * @param[in] i2cAddr - PSU i2c address
55  *
56  * @return Pointer to PSU PMBus interface
57  */
58 std::unique_ptr<phosphor::pmbus::PMBusBase>
59     getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr);
60 
61 /**
62  * @brief Reads a VPD value from PMBus, corrects size, and contents.
63  *
64  * If the VPD data read is not the passed in size, resize and fill with
65  * spaces. If the data contains a non-alphanumeric value, replace any of
66  * those values with spaces.
67  *
68  * @param[in] pmbusIntf - PMBus Interface.
69  * @param[in] vpdName - The name of the sysfs "file" to read data from.
70  * @param[in] type - The HWMON file type to read from.
71  * @param[in] vpdSize - The expected size of the data for this VPD/property
72  *
73  * @return A string containing the VPD data read, resized if necessary
74  */
75 std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
76                          const std::string& vpdName,
77                          const phosphor::pmbus::Type& type,
78                          const std::size_t& vpdSize);
79 
80 /**
81  * @brief Check for file existence
82  *
83  * @param[in] filePath - File path
84  *
85  * @return bool
86  */
87 bool checkFileExists(const std::string& filePath);
88 
89 /**
90  * @brief Get the device name from the device path
91  *
92  * @param[in] devPath - PSU path
93  *
94  * @return device name e.g. 3-0068
95  */
96 std::string getDeviceName(std::string devPath);
97 
98 /**
99  * @brief Function to get device path using DBus bus and PSU
100  * inventory Path
101  *
102  * @param[in] bus - The sdbusplus DBus bus connection
103  * @param[in] psuInventoryPath - PSU inventory path
104  *
105  * @return device path e.g. /sys/bus/i2c/devices/3-0068
106  */
107 std::string getDevicePath(sdbusplus::bus_t& bus,
108                           const std::string& psuInventoryPath);
109 
110 /**
111  * @brief Parse the device name to obtain bus and device address
112  *
113  * @param[in] devName - Device name
114  *
115  * @return bus and device address
116  */
117 std::pair<uint8_t, uint8_t> parseDeviceName(const std::string& devName);
118 
119 /**
120  * @brief Wrapper to check existence of PSU JSON file
121  *
122  * @return true or false (true if using JSON file)
123  */
124 bool usePsuJsonFile();
125 
126 } // namespace utils
127