blob: 534d113101c09b65a3a38a00729fba04a1c1a7a1 [file] [log] [blame]
Lei YU0bf1b782019-08-29 16:02:30 +08001/**
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
Faisal Awada5dce1a72024-08-19 15:51:44 -050018#include "pmbus.hpp"
19
20#include <sdbusplus/bus.hpp>
21
22#include <memory>
Lei YU0bf1b782019-08-29 16:02:30 +080023#include <string>
Faisal Awada5dce1a72024-08-19 15:51:44 -050024#include <tuple>
Lei YU093b5912019-10-22 15:28:51 +080025#include <vector>
Lei YU0bf1b782019-08-29 16:02:30 +080026
27namespace version
28{
Faisal Awada5dce1a72024-08-19 15:51:44 -050029namespace utils
30{
31// PsuInfo contains the device path, pmbus read type, and the version string
32using PsuVersionInfo =
33 std::tuple<std::string, phosphor::pmbus::Type, std::string>;
34
35// PsuI2cInfo contains the device i2c bus and i2c address
36using PsuI2cInfo = std::tuple<std::uint64_t, std::uint64_t>;
Lei YU0bf1b782019-08-29 16:02:30 +080037
38/**
Faisal Awada5dce1a72024-08-19 15:51:44 -050039 * @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 */
45PsuVersionInfo 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 */
54std::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 */
64PsuI2cInfo 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 */
75std::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 */
92std::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 */
104bool 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 */
116std::string getVersion(sdbusplus::bus_t& bus,
117 const std::string& psuInventoryPath);
118
119/**
120 * Get the software version of the PSU using psu.json
Lei YU0bf1b782019-08-29 16:02:30 +0800121 *
122 * @param[in] psuInventoryPath - The inventory path of the PSU
Lei YU093b5912019-10-22 15:28:51 +0800123 *
124 * @return The version of the PSU
Lei YU0bf1b782019-08-29 16:02:30 +0800125 */
126std::string getVersion(const std::string& psuInventoryPath);
127
Lei YU093b5912019-10-22 15:28:51 +0800128/**
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 */
135std::string getLatest(const std::vector<std::string>& versions);
136
Lei YU0bf1b782019-08-29 16:02:30 +0800137} // namespace version