blob: 12bb7c9aadd845c34b26defd2a0aa0ab9f14aed2 [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#include "config.h"
17
18#include "version.hpp"
19
20#include "pmbus.hpp"
21#include "utility.hpp"
22
23#include <phosphor-logging/log.hpp>
24#include <tuple>
25
26using json = nlohmann::json;
27
28using namespace phosphor::logging;
29
30// PsuInfo contains the device path, pmbus read type, and the version string
31using PsuVersionInfo =
32 std::tuple<std::string, phosphor::pmbus::Type, std::string>;
33
34namespace utils
35{
36PsuVersionInfo getVersionInfo(const std::string& psuInventoryPath)
37{
38 auto data = phosphor::power::util::loadJSONFromFile(PSU_JSON_PATH);
39
40 if (data == nullptr)
41 {
42 return {};
43 }
44
45 auto devices = data.find("psuDevices");
46 if (devices == data.end())
47 {
48 log<level::WARNING>("Unable to find psuDevices");
49 return {};
50 }
51 auto devicePath = devices->find(psuInventoryPath);
52 if (devicePath == devices->end())
53 {
54 log<level::WARNING>("Unable to find path for PSU",
55 entry("PATH=%s", psuInventoryPath.c_str()));
56 return {};
57 }
58
59 auto type = phosphor::power::util::getPMBusAccessType(data);
60
61 std::string versionStr;
62 for (const auto& fru : data["fruConfigs"])
63 {
64 if (fru["propertyName"] == "Version")
65 {
66 versionStr = fru["fileName"];
67 break;
68 }
69 }
70 if (versionStr.empty())
71 {
72 log<level::WARNING>("Unable to find Version file");
73 return {};
74 }
75 return std::make_tuple(*devicePath, type, versionStr);
76}
Lei YU093b5912019-10-22 15:28:51 +080077
78// A default implemention compare the string itself
79std::string getLatestDefault(const std::vector<std::string>& versions)
80{
81 std::string latest;
82 for (const auto& version : versions)
83 {
84 if (latest < version)
85 {
86 latest = version;
87 }
88 }
89 return latest;
90}
91
Lei YU0bf1b782019-08-29 16:02:30 +080092} // namespace utils
93
94namespace version
95{
96
97std::string getVersion(const std::string& psuInventoryPath)
98{
99 const auto& [devicePath, type, versionStr] =
100 utils::getVersionInfo(psuInventoryPath);
101 if (devicePath.empty() || versionStr.empty())
102 {
103 return {};
104 }
105 std::string version;
106 try
107 {
108 phosphor::pmbus::PMBus pmbus(devicePath);
109 version = pmbus.readString(versionStr, type);
110 }
111 catch (const std::exception& ex)
112 {
113 log<level::ERR>(ex.what());
114 }
115 return version;
116}
117
Lei YU093b5912019-10-22 15:28:51 +0800118std::string getLatest(const std::vector<std::string>& versions)
119{
120 // TODO: when multiple PSU/Machines are supported, add configuration options
121 // to implement machine-specific logic.
122 // For now IBM AC Servers and Inspur FP5280G2 are supported.
123 //
124 // IBM AC servers' PSU version has two types:
125 // * XXXXYYYYZZZZ: XXXX is the primary version
126 // YYYY is the secondary version
127 // ZZZZ is the communication version
128 //
129 // * XXXXYYYY: XXXX is the primary version
130 // YYYY is the seconday version
131 //
132 // Inspur FP5280G2 PSU version is human readable text and a larger string
133 // means a newer version.
134 //
135 // So just compare by strings is OK for these cases
136 return utils::getLatestDefault(versions);
137}
138
Lei YU0bf1b782019-08-29 16:02:30 +0800139} // namespace version