1 /**
2  * Copyright © 2019 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 <memory>
23 #include <string>
24 #include <tuple>
25 #include <vector>
26 
27 namespace version
28 {
29 namespace utils
30 {
31 // PsuInfo contains the device path, pmbus read type, and the version string
32 using PsuVersionInfo =
33     std::tuple<std::string, phosphor::pmbus::Type, std::string>;
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 PSU version information
40  *
41  * @param[in] psuInventoryPath - The PSU inventory path.
42  *
43  * @return tuple - device path, pmbus read type and PSU version
44  */
45 PsuVersionInfo getVersionInfo(const std::string& psuInventoryPath);
46 
47 /**
48  * @brief Get firmware latest version
49  *
50  * @param[in] versions - String of versions
51  *
52  * @return version - latest firmware level
53  */
54 std::string getLatestDefault(const std::vector<std::string>& versions);
55 
56 /**
57  * @brief Get i2c bus and address
58  *
59  * @param[in] bus - Systemd bus connection
60  * @param[in] psuInventoryPath - The PSU inventory path.
61  *
62  * @return tuple - i2cBus and i2cAddr.
63  */
64 PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus,
65                      const std::string& psuInventoryPath);
66 
67 /**
68  * @brief Get PMBus interface pointer
69  *
70  * @param[in] i2cBus - PSU i2c bus
71  * @param[in] i2cAddr - PSU i2c address
72  *
73  * @return Pointer to PSU PMBus interface
74  */
75 std::unique_ptr<phosphor::pmbus::PMBusBase>
76     getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr);
77 
78 /**
79  * @brief Reads a VPD value from PMBus, corrects size, and contents.
80  *
81  * If the VPD data read is not the passed in size, resize and fill with
82  * spaces. If the data contains a non-alphanumeric value, replace any of
83  * those values with spaces.
84  *
85  * @param[in] pmbusIntf - PMBus Interface.
86  * @param[in] vpdName - The name of the sysfs "file" to read data from.
87  * @param[in] type - The HWMON file type to read from.
88  * @param[in] vpdSize - The expected size of the data for this VPD/property
89  *
90  * @return A string containing the VPD data read, resized if necessary
91  */
92 std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
93                          const std::string& vpdName,
94                          const phosphor::pmbus::Type& type,
95                          const std::size_t& vpdSize);
96 
97 /**
98  * @brief Check for file existence
99  *
100  * @param[in] filePath - File path
101  *
102  * @return bool
103  */
104 bool checkFileExists(const std::string& filePath);
105 
106 } // namespace utils
107 
108 /**
109  * Get the software version of the PSU using sysfs
110  *
111  * @param[in] bus - Systemd bus connection
112  * @param[in] psuInventoryPath - The inventory path of the PSU
113  *
114  * @return The version of the PSU
115  */
116 std::string getVersion(sdbusplus::bus_t& bus,
117                        const std::string& psuInventoryPath);
118 
119 /**
120  * Get the software version of the PSU using psu.json
121  *
122  * @param[in] psuInventoryPath - The inventory path of the PSU
123  *
124  * @return The version of the PSU
125  */
126 std::string getVersion(const std::string& psuInventoryPath);
127 
128 /**
129  * Get the latest version from a list of versions
130  *
131  * @param[in] versions - The list of PSU version strings
132  *
133  * @return The latest version
134  */
135 std::string getLatest(const std::vector<std::string>& versions);
136 
137 } // namespace version
138