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 
27 /**
28  * @namespace utils
29  *
30  * Contains utility functions used within the psutils tool.
31  */
32 namespace utils
33 {
34 
35 // PsuI2cInfo contains the device i2c bus and i2c address
36 using PsuI2cInfo = std::tuple<std::uint64_t, std::uint64_t>;
37 
38 /**
39  * @brief Get i2c bus and address
40  *
41  * @param[in] bus - Systemd bus connection
42  * @param[in] psuInventoryPath - The PSU inventory path.
43  *
44  * @return tuple - i2cBus and i2cAddr.
45  */
46 PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus,
47                      const std::string& psuInventoryPath);
48 
49 /**
50  * @brief Get PMBus interface pointer
51  *
52  * @param[in] i2cBus - PSU i2c bus
53  * @param[in] i2cAddr - PSU i2c address
54  *
55  * @return Pointer to PSU PMBus interface
56  */
57 std::unique_ptr<phosphor::pmbus::PMBusBase>
58     getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr);
59 
60 /**
61  * @brief Reads a VPD value from PMBus, corrects size, and contents.
62  *
63  * If the VPD data read is not the passed in size, resize and fill with
64  * spaces. If the data contains a non-alphanumeric value, replace any of
65  * those values with spaces.
66  *
67  * @param[in] pmbusIntf - PMBus Interface.
68  * @param[in] vpdName - The name of the sysfs "file" to read data from.
69  * @param[in] type - The HWMON file type to read from.
70  * @param[in] vpdSize - The expected size of the data for this VPD/property
71  *
72  * @return A string containing the VPD data read, resized if necessary
73  */
74 std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
75                          const std::string& vpdName,
76                          const phosphor::pmbus::Type& type,
77                          const std::size_t& vpdSize);
78 
79 /**
80  * @brief Check for file existence
81  *
82  * @param[in] filePath - File path
83  *
84  * @return bool
85  */
86 bool checkFileExists(const std::string& filePath);
87 
88 } // namespace utils
89