| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "config.h" |
| 4 | |
| 5 | #include "constants.hpp" |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 6 | #include "event_logger.hpp" |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 7 | #include "exceptions.hpp" |
| 8 | #include "logger.hpp" |
| 9 | #include "types.hpp" |
| 10 | |
| 11 | #include <nlohmann/json.hpp> |
| 12 | #include <utility/common_utility.hpp> |
| 13 | #include <utility/dbus_utility.hpp> |
| 14 | |
| 15 | #include <filesystem> |
| 16 | #include <fstream> |
| 17 | #include <regex> |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 18 | #include <typeindex> |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 19 | |
| 20 | namespace vpd |
| 21 | { |
| 22 | namespace vpdSpecificUtility |
| 23 | { |
| 24 | /** |
| 25 | * @brief API to generate file name for bad VPD. |
| 26 | * |
| 27 | * For i2c eeproms - the pattern of the vpd-name will be |
| 28 | * i2c-<bus-number>-<eeprom-address>. |
| 29 | * For spi eeproms - the pattern of the vpd-name will be spi-<spi-number>. |
| 30 | * |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 31 | * @param[in] i_vpdFilePath - file path of the vpd. |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 32 | * @param[out] o_errCode - to set error code in case of error. |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 33 | * |
| 34 | * @return On success, returns generated file name, otherwise returns empty |
| 35 | * string. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 36 | */ |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 37 | inline std::string generateBadVPDFileName(const std::string& i_vpdFilePath, |
| 38 | uint16_t& o_errCode) noexcept |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 39 | { |
| Souvik Roy | 0f37043 | 2025-04-08 01:55:58 -0500 | [diff] [blame] | 40 | std::string l_badVpdFileName{constants::badVpdDir}; |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 41 | |
| 42 | if (i_vpdFilePath.empty()) |
| 43 | { |
| 44 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 45 | return l_badVpdFileName; |
| 46 | } |
| 47 | |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 48 | try |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 49 | { |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 50 | if (i_vpdFilePath.find("i2c") != std::string::npos) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 51 | { |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 52 | l_badVpdFileName += "i2c-"; |
| 53 | std::regex l_i2cPattern("(at24/)([0-9]+-[0-9]+)\\/"); |
| 54 | std::smatch l_match; |
| 55 | if (std::regex_search(i_vpdFilePath, l_match, l_i2cPattern)) |
| 56 | { |
| 57 | l_badVpdFileName += l_match.str(2); |
| 58 | } |
| 59 | } |
| 60 | else if (i_vpdFilePath.find("spi") != std::string::npos) |
| 61 | { |
| 62 | std::regex l_spiPattern("((spi)[0-9]+)(.0)"); |
| 63 | std::smatch l_match; |
| 64 | if (std::regex_search(i_vpdFilePath, l_match, l_spiPattern)) |
| 65 | { |
| 66 | l_badVpdFileName += l_match.str(1); |
| 67 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 68 | } |
| 69 | } |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 70 | catch (const std::exception& l_ex) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 71 | { |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 72 | l_badVpdFileName.clear(); |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 73 | o_errCode = error_code::STANDARD_EXCEPTION; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 74 | } |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 75 | return l_badVpdFileName; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @brief API which dumps the broken/bad vpd in a directory. |
| 80 | * When the vpd is bad, this API places the bad vpd file inside |
| Souvik Roy | 0f37043 | 2025-04-08 01:55:58 -0500 | [diff] [blame] | 81 | * "/var/lib/vpd/dumps" in BMC, in order to collect bad VPD data as a part of |
| 82 | * user initiated BMC dump. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 83 | * |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 84 | * |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 85 | * @param[in] i_vpdFilePath - vpd file path |
| 86 | * @param[in] i_vpdVector - vpd vector |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 87 | * @param[out] o_errCode - To set error code in case of error. |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 88 | * |
| 89 | * @return On success returns 0, otherwise returns -1. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 90 | */ |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 91 | inline int dumpBadVpd(const std::string& i_vpdFilePath, |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 92 | const types::BinaryVector& i_vpdVector, |
| 93 | uint16_t& o_errCode) noexcept |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 94 | { |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 95 | if (i_vpdFilePath.empty() || i_vpdVector.empty()) |
| 96 | { |
| 97 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 98 | return constants::FAILURE; |
| 99 | } |
| 100 | |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 101 | int l_rc{constants::FAILURE}; |
| 102 | try |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 103 | { |
| Souvik Roy | 0f37043 | 2025-04-08 01:55:58 -0500 | [diff] [blame] | 104 | std::filesystem::create_directory(constants::badVpdDir); |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 105 | auto l_badVpdPath = generateBadVPDFileName(i_vpdFilePath, o_errCode); |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 106 | |
| 107 | if (l_badVpdPath.empty()) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 108 | { |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 109 | if (o_errCode) |
| 110 | { |
| 111 | logging::logMessage("Failed to create bad VPD file name : " + |
| 112 | commonUtility::getErrCodeMsg(o_errCode)); |
| 113 | } |
| 114 | |
| 115 | return constants::FAILURE; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 116 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 117 | |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 118 | if (std::filesystem::exists(l_badVpdPath)) |
| 119 | { |
| 120 | std::error_code l_ec; |
| 121 | std::filesystem::remove(l_badVpdPath, l_ec); |
| 122 | if (l_ec) // error code |
| 123 | { |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 124 | o_errCode = error_code::FILE_SYSTEM_ERROR; |
| 125 | return constants::FAILURE; |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | std::ofstream l_badVpdFileStream(l_badVpdPath, std::ofstream::binary); |
| 130 | if (!l_badVpdFileStream.is_open()) |
| 131 | { |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 132 | o_errCode = error_code::FILE_ACCESS_ERROR; |
| 133 | return constants::FAILURE; |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | l_badVpdFileStream.write( |
| 137 | reinterpret_cast<const char*>(i_vpdVector.data()), |
| 138 | i_vpdVector.size()); |
| 139 | |
| 140 | l_rc = constants::SUCCESS; |
| 141 | } |
| 142 | catch (const std::exception& l_ex) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 143 | { |
| Rekha Aparna | 7f6cd4c | 2025-10-06 23:44:04 -0500 | [diff] [blame] | 144 | o_errCode = error_code::STANDARD_EXCEPTION; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 145 | } |
| Souvik Roy | 8fc1252 | 2025-02-19 01:28:38 -0600 | [diff] [blame] | 146 | return l_rc; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @brief An API to read value of a keyword. |
| 151 | * |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 152 | * |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 153 | * @param[in] i_kwdValueMap - A map having Kwd value pair. |
| 154 | * @param[in] i_kwd - keyword name. |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 155 | * @param[out] o_errCode - To set error code in case of error. |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 156 | * |
| 157 | * @return On success returns value of the keyword read from map, otherwise |
| 158 | * returns empty string. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 159 | */ |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 160 | inline std::string getKwVal(const types::IPZKwdValueMap& i_kwdValueMap, |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 161 | const std::string& i_kwd, |
| 162 | uint16_t& o_errCode) noexcept |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 163 | { |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 164 | std::string l_kwdValue; |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 165 | if (i_kwd.empty() || i_kwdValueMap.empty()) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 166 | { |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 167 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 168 | return l_kwdValue; |
| 169 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 170 | |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 171 | auto l_itrToKwd = i_kwdValueMap.find(i_kwd); |
| 172 | if (l_itrToKwd != i_kwdValueMap.end()) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 173 | { |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 174 | l_kwdValue = l_itrToKwd->second; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 175 | } |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 176 | else |
| 177 | { |
| 178 | o_errCode = error_code::KEYWORD_NOT_FOUND; |
| 179 | } |
| 180 | |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 181 | return l_kwdValue; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * @brief An API to process encoding of a keyword. |
| 186 | * |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 187 | * @param[in] i_keyword - Keyword to be processed. |
| 188 | * @param[in] i_encoding - Type of encoding. |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 189 | * @param[out] o_errCode - To set error code in case of error. |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 190 | * |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 191 | * @return Value after being processed for encoded type. |
| 192 | */ |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 193 | inline std::string encodeKeyword(const std::string& i_keyword, |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 194 | const std::string& i_encoding, |
| 195 | uint16_t& o_errCode) noexcept |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 196 | { |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 197 | if (i_keyword.empty()) |
| 198 | { |
| 199 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 200 | return std::string{}; |
| 201 | } |
| 202 | |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 203 | // Default value is keyword value |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 204 | std::string l_result(i_keyword.begin(), i_keyword.end()); |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 205 | |
| 206 | if (i_encoding.empty()) |
| 207 | { |
| 208 | return l_result; |
| 209 | } |
| 210 | |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 211 | try |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 212 | { |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 213 | if (i_encoding == "MAC") |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 214 | { |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 215 | l_result.clear(); |
| 216 | size_t l_firstByte = i_keyword[0]; |
| Rekha Aparna | bffecd9 | 2025-04-08 13:01:41 -0500 | [diff] [blame] | 217 | |
| 218 | auto l_hexValue = commonUtility::toHex(l_firstByte >> 4); |
| 219 | |
| 220 | if (!l_hexValue) |
| 221 | { |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 222 | o_errCode = error_code::OUT_OF_BOUND_EXCEPTION; |
| 223 | return std::string{}; |
| Rekha Aparna | bffecd9 | 2025-04-08 13:01:41 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | l_result += l_hexValue; |
| 227 | |
| 228 | l_hexValue = commonUtility::toHex(l_firstByte & 0x0f); |
| 229 | |
| 230 | if (!l_hexValue) |
| 231 | { |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 232 | o_errCode = error_code::OUT_OF_BOUND_EXCEPTION; |
| 233 | return std::string{}; |
| Rekha Aparna | bffecd9 | 2025-04-08 13:01:41 -0500 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | l_result += l_hexValue; |
| 237 | |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 238 | for (size_t i = 1; i < i_keyword.size(); ++i) |
| 239 | { |
| 240 | l_result += ":"; |
| Rekha Aparna | bffecd9 | 2025-04-08 13:01:41 -0500 | [diff] [blame] | 241 | |
| 242 | l_hexValue = commonUtility::toHex(i_keyword[i] >> 4); |
| 243 | |
| 244 | if (!l_hexValue) |
| 245 | { |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 246 | o_errCode = error_code::OUT_OF_BOUND_EXCEPTION; |
| 247 | return std::string{}; |
| Rekha Aparna | bffecd9 | 2025-04-08 13:01:41 -0500 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | l_result += l_hexValue; |
| 251 | |
| 252 | l_hexValue = commonUtility::toHex(i_keyword[i] & 0x0f); |
| 253 | |
| 254 | if (!l_hexValue) |
| 255 | { |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 256 | o_errCode = error_code::OUT_OF_BOUND_EXCEPTION; |
| 257 | return std::string{}; |
| Rekha Aparna | bffecd9 | 2025-04-08 13:01:41 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | l_result += l_hexValue; |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | else if (i_encoding == "DATE") |
| 264 | { |
| 265 | // Date, represent as |
| 266 | // <year>-<month>-<day> <hour>:<min> |
| 267 | l_result.clear(); |
| 268 | static constexpr uint8_t skipPrefix = 3; |
| 269 | |
| 270 | auto strItr = i_keyword.begin(); |
| 271 | advance(strItr, skipPrefix); |
| 272 | for_each(strItr, i_keyword.end(), |
| 273 | [&l_result](size_t c) { l_result += c; }); |
| 274 | |
| 275 | l_result.insert(constants::BD_YEAR_END, 1, '-'); |
| 276 | l_result.insert(constants::BD_MONTH_END, 1, '-'); |
| 277 | l_result.insert(constants::BD_DAY_END, 1, ' '); |
| 278 | l_result.insert(constants::BD_HOUR_END, 1, ':'); |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 279 | } |
| 280 | } |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 281 | catch (const std::exception& l_ex) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 282 | { |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 283 | l_result.clear(); |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 284 | o_errCode = error_code::STANDARD_EXCEPTION; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| Souvik Roy | f277d6a | 2025-02-20 00:08:43 -0600 | [diff] [blame] | 287 | return l_result; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @brief Helper function to insert or merge in map. |
| 292 | * |
| 293 | * This method checks in an interface if the given interface exists. If the |
| 294 | * interface key already exists, property map is inserted corresponding to it. |
| 295 | * If the key does'nt exist then given interface and property map pair is newly |
| 296 | * created. If the property present in propertymap already exist in the |
| 297 | * InterfaceMap, then the new property value is ignored. |
| 298 | * |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 299 | * @param[in,out] io_map - Interface map. |
| 300 | * @param[in] i_interface - Interface to be processed. |
| 301 | * @param[in] i_propertyMap - new property map that needs to be emplaced. |
| Rekha Aparna | d366222 | 2025-10-14 10:33:17 -0500 | [diff] [blame] | 302 | * @param[out] o_errCode - To set error code in case of error. |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 303 | * |
| 304 | * @return On success returns 0, otherwise returns -1. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 305 | */ |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 306 | inline int insertOrMerge(types::InterfaceMap& io_map, |
| 307 | const std::string& i_interface, |
| Rekha Aparna | d366222 | 2025-10-14 10:33:17 -0500 | [diff] [blame] | 308 | types::PropertyMap&& i_propertyMap, |
| 309 | uint16_t& o_errCode) noexcept |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 310 | { |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 311 | int l_rc{constants::FAILURE}; |
| Rekha Aparna | d366222 | 2025-10-14 10:33:17 -0500 | [diff] [blame] | 312 | |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 313 | try |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 314 | { |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 315 | if (io_map.find(i_interface) != io_map.end()) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 316 | { |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 317 | auto& l_prop = io_map.at(i_interface); |
| 318 | std::for_each(i_propertyMap.begin(), i_propertyMap.end(), |
| 319 | [&l_prop](auto l_keyValue) { |
| 320 | l_prop[l_keyValue.first] = l_keyValue.second; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 321 | }); |
| 322 | } |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 323 | else |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 324 | { |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 325 | io_map.emplace(i_interface, i_propertyMap); |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 326 | } |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 327 | |
| 328 | l_rc = constants::SUCCESS; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 329 | } |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 330 | catch (const std::exception& l_ex) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 331 | { |
| Rekha Aparna | d366222 | 2025-10-14 10:33:17 -0500 | [diff] [blame] | 332 | o_errCode = error_code::STANDARD_EXCEPTION; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 333 | } |
| Souvik Roy | fa47e6c | 2025-02-20 00:17:25 -0600 | [diff] [blame] | 334 | return l_rc; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /** |
| 338 | * @brief API to expand unpanded location code. |
| 339 | * |
| 340 | * Note: The API handles all the exception internally, in case of any error |
| 341 | * unexpanded location code will be returned as it is. |
| 342 | * |
| 343 | * @param[in] unexpandedLocationCode - Unexpanded location code. |
| 344 | * @param[in] parsedVpdMap - Parsed VPD map. |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 345 | * @param[out] o_errCode - To set error code in case of error. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 346 | * @return Expanded location code. In case of any error, unexpanded is returned |
| 347 | * as it is. |
| 348 | */ |
| Patrick Williams | 43fedab | 2025-02-03 14:28:05 -0500 | [diff] [blame] | 349 | inline std::string getExpandedLocationCode( |
| 350 | const std::string& unexpandedLocationCode, |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 351 | const types::VPDMapVariant& parsedVpdMap, uint16_t& o_errCode) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 352 | { |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 353 | if (unexpandedLocationCode.empty() || |
| 354 | std::holds_alternative<std::monostate>(parsedVpdMap)) |
| 355 | { |
| 356 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 357 | return unexpandedLocationCode; |
| 358 | } |
| 359 | |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 360 | auto expanded{unexpandedLocationCode}; |
| 361 | |
| 362 | try |
| 363 | { |
| 364 | // Expanded location code is formed by combining two keywords |
| 365 | // depending on type in unexpanded one. Second one is always "SE". |
| 366 | std::string kwd1, kwd2{constants::kwdSE}; |
| 367 | |
| 368 | // interface to search for required keywords; |
| 369 | std::string kwdInterface; |
| 370 | |
| 371 | // record which holds the required keywords. |
| 372 | std::string recordName; |
| 373 | |
| 374 | auto pos = unexpandedLocationCode.find("fcs"); |
| 375 | if (pos != std::string::npos) |
| 376 | { |
| 377 | kwd1 = constants::kwdFC; |
| 378 | kwdInterface = constants::vcenInf; |
| 379 | recordName = constants::recVCEN; |
| 380 | } |
| 381 | else |
| 382 | { |
| 383 | pos = unexpandedLocationCode.find("mts"); |
| 384 | if (pos != std::string::npos) |
| 385 | { |
| 386 | kwd1 = constants::kwdTM; |
| 387 | kwdInterface = constants::vsysInf; |
| 388 | recordName = constants::recVSYS; |
| 389 | } |
| 390 | else |
| 391 | { |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 392 | o_errCode = error_code::FAILED_TO_DETECT_LOCATION_CODE_TYPE; |
| 393 | return expanded; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
| 397 | std::string firstKwdValue, secondKwdValue; |
| 398 | |
| 399 | if (auto ipzVpdMap = std::get_if<types::IPZVpdMap>(&parsedVpdMap); |
| 400 | ipzVpdMap && (*ipzVpdMap).find(recordName) != (*ipzVpdMap).end()) |
| 401 | { |
| 402 | auto itrToVCEN = (*ipzVpdMap).find(recordName); |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 403 | firstKwdValue = getKwVal(itrToVCEN->second, kwd1, o_errCode); |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 404 | if (firstKwdValue.empty()) |
| 405 | { |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 406 | o_errCode = error_code::KEYWORD_NOT_FOUND; |
| 407 | return expanded; |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 408 | } |
| 409 | |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 410 | secondKwdValue = getKwVal(itrToVCEN->second, kwd2, o_errCode); |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 411 | if (secondKwdValue.empty()) |
| 412 | { |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 413 | o_errCode = error_code::KEYWORD_NOT_FOUND; |
| 414 | return expanded; |
| Souvik Roy | a55fcca | 2025-02-19 01:33:58 -0600 | [diff] [blame] | 415 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 416 | } |
| 417 | else |
| 418 | { |
| Anupama B R | 68a7043 | 2025-09-25 02:09:37 -0500 | [diff] [blame] | 419 | std::vector<std::string> interfaceList = {kwdInterface}; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 420 | |
| 421 | types::MapperGetObject mapperRetValue = dbusUtility::getObjectMap( |
| 422 | std::string(constants::systemVpdInvPath), interfaceList); |
| 423 | |
| 424 | if (mapperRetValue.empty()) |
| 425 | { |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 426 | o_errCode = error_code::DBUS_FAILURE; |
| 427 | return expanded; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | const std::string& serviceName = std::get<0>(mapperRetValue.at(0)); |
| 431 | |
| 432 | auto retVal = dbusUtility::readDbusProperty( |
| 433 | serviceName, std::string(constants::systemVpdInvPath), |
| 434 | kwdInterface, kwd1); |
| 435 | |
| 436 | if (auto kwdVal = std::get_if<types::BinaryVector>(&retVal)) |
| 437 | { |
| 438 | firstKwdValue.assign( |
| 439 | reinterpret_cast<const char*>(kwdVal->data()), |
| 440 | kwdVal->size()); |
| 441 | } |
| 442 | else |
| 443 | { |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 444 | o_errCode = error_code::RECEIVED_INVALID_KWD_TYPE_FROM_DBUS; |
| 445 | return expanded; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | retVal = dbusUtility::readDbusProperty( |
| 449 | serviceName, std::string(constants::systemVpdInvPath), |
| 450 | kwdInterface, kwd2); |
| 451 | |
| 452 | if (auto kwdVal = std::get_if<types::BinaryVector>(&retVal)) |
| 453 | { |
| 454 | secondKwdValue.assign( |
| 455 | reinterpret_cast<const char*>(kwdVal->data()), |
| 456 | kwdVal->size()); |
| 457 | } |
| 458 | else |
| 459 | { |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 460 | o_errCode = error_code::RECEIVED_INVALID_KWD_TYPE_FROM_DBUS; |
| 461 | return expanded; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | if (unexpandedLocationCode.find("fcs") != std::string::npos) |
| 466 | { |
| 467 | // TODO: See if ND0 can be placed in the JSON |
| 468 | expanded.replace( |
| 469 | pos, 3, firstKwdValue.substr(0, 4) + ".ND0." + secondKwdValue); |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | replace(firstKwdValue.begin(), firstKwdValue.end(), '-', '.'); |
| 474 | expanded.replace(pos, 3, firstKwdValue + "." + secondKwdValue); |
| 475 | } |
| 476 | } |
| 477 | catch (const std::exception& ex) |
| 478 | { |
| Rekha Aparna | 6256db9 | 2025-10-17 09:58:49 -0500 | [diff] [blame] | 479 | o_errCode = error_code::STANDARD_EXCEPTION; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | return expanded; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * @brief An API to get VPD in a vector. |
| 487 | * |
| 488 | * The vector is required by the respective parser to fill the VPD map. |
| 489 | * Note: API throws exception in case of failure. Caller needs to handle. |
| 490 | * |
| 491 | * @param[in] vpdFilePath - EEPROM path of the FRU. |
| 492 | * @param[out] vpdVector - VPD in vector form. |
| 493 | * @param[in] vpdStartOffset - Offset of VPD data in EEPROM. |
| Rekha Aparna | 769d657 | 2025-10-22 09:50:35 -0500 | [diff] [blame] | 494 | * @param[out] o_errCode - To set error code in case of error. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 495 | */ |
| 496 | inline void getVpdDataInVector(const std::string& vpdFilePath, |
| 497 | types::BinaryVector& vpdVector, |
| Rekha Aparna | 769d657 | 2025-10-22 09:50:35 -0500 | [diff] [blame] | 498 | size_t& vpdStartOffset, uint16_t& o_errCode) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 499 | { |
| Rekha Aparna | 769d657 | 2025-10-22 09:50:35 -0500 | [diff] [blame] | 500 | if (vpdFilePath.empty()) |
| 501 | { |
| 502 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 503 | return; |
| 504 | } |
| 505 | |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 506 | try |
| 507 | { |
| 508 | std::fstream vpdFileStream; |
| 509 | vpdFileStream.exceptions( |
| 510 | std::ifstream::badbit | std::ifstream::failbit); |
| 511 | vpdFileStream.open(vpdFilePath, std::ios::in | std::ios::binary); |
| 512 | auto vpdSizeToRead = std::min(std::filesystem::file_size(vpdFilePath), |
| 513 | static_cast<uintmax_t>(65504)); |
| 514 | vpdVector.resize(vpdSizeToRead); |
| 515 | |
| 516 | vpdFileStream.seekg(vpdStartOffset, std::ios_base::beg); |
| 517 | vpdFileStream.read(reinterpret_cast<char*>(&vpdVector[0]), |
| 518 | vpdSizeToRead); |
| 519 | |
| 520 | vpdVector.resize(vpdFileStream.gcount()); |
| 521 | vpdFileStream.clear(std::ios_base::eofbit); |
| 522 | } |
| 523 | catch (const std::ifstream::failure& fail) |
| 524 | { |
| Rekha Aparna | 769d657 | 2025-10-22 09:50:35 -0500 | [diff] [blame] | 525 | o_errCode = error_code::FILE_SYSTEM_ERROR; |
| 526 | return; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * @brief An API to get D-bus representation of given VPD keyword. |
| 532 | * |
| 533 | * @param[in] i_keywordName - VPD keyword name. |
| Rekha Aparna | 769d657 | 2025-10-22 09:50:35 -0500 | [diff] [blame] | 534 | * @param[out] o_errCode - To set error code in case of error. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 535 | * |
| 536 | * @return D-bus representation of given keyword. |
| 537 | */ |
| Rekha Aparna | 769d657 | 2025-10-22 09:50:35 -0500 | [diff] [blame] | 538 | inline std::string getDbusPropNameForGivenKw(const std::string& i_keywordName, |
| 539 | uint16_t& o_errCode) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 540 | { |
| Rekha Aparna | 769d657 | 2025-10-22 09:50:35 -0500 | [diff] [blame] | 541 | if (i_keywordName.empty()) |
| 542 | { |
| 543 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 544 | return std::string{}; |
| 545 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 546 | // Check for "#" prefixed VPD keyword. |
| 547 | if ((i_keywordName.size() == vpd::constants::TWO_BYTES) && |
| 548 | (i_keywordName.at(0) == constants::POUND_KW)) |
| 549 | { |
| 550 | // D-bus doesn't support "#". Replace "#" with "PD_" for those "#" |
| 551 | // prefixed keywords. |
| 552 | return (std::string(constants::POUND_KW_PREFIX) + |
| 553 | i_keywordName.substr(1)); |
| 554 | } |
| 555 | |
| 556 | // Return the keyword name back, if D-bus representation is same as the VPD |
| 557 | // keyword name. |
| 558 | return i_keywordName; |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * @brief API to find CCIN in parsed VPD map. |
| 563 | * |
| 564 | * Few FRUs need some special handling. To identify those FRUs CCIN are used. |
| 565 | * The API will check from parsed VPD map if the FRU is the one with desired |
| 566 | * CCIN. |
| 567 | * |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 568 | * @param[in] i_JsonObject - Any JSON which contains CCIN tag to match. |
| 569 | * @param[in] i_parsedVpdMap - Parsed VPD map. |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 570 | * |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 571 | * @return True if found, false otherwise. |
| 572 | */ |
| 573 | inline bool findCcinInVpd(const nlohmann::json& i_JsonObject, |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 574 | const types::VPDMapVariant& i_parsedVpdMap) noexcept |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 575 | { |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 576 | bool l_rc{false}; |
| 577 | try |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 578 | { |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 579 | if (i_JsonObject.empty()) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 580 | { |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 581 | throw std::runtime_error("Json object is empty. Can't find CCIN"); |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 584 | if (auto l_ipzVPDMap = std::get_if<types::IPZVpdMap>(&i_parsedVpdMap)) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 585 | { |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 586 | auto l_itrToRec = (*l_ipzVPDMap).find("VINI"); |
| 587 | if (l_itrToRec == (*l_ipzVPDMap).end()) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 588 | { |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 589 | throw DataException( |
| 590 | "VINI record not found in parsed VPD. Can't find CCIN"); |
| 591 | } |
| 592 | |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 593 | uint16_t l_errCode = 0; |
| 594 | std::string l_ccinFromVpd{vpdSpecificUtility::getKwVal( |
| 595 | l_itrToRec->second, "CC", l_errCode)}; |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 596 | if (l_ccinFromVpd.empty()) |
| 597 | { |
| Rekha Aparna | 7d9a706 | 2025-10-07 04:14:42 -0500 | [diff] [blame] | 598 | throw DataException("Can't find CCIN, error : " + |
| 599 | commonUtility::getErrCodeMsg(l_errCode)); |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | transform(l_ccinFromVpd.begin(), l_ccinFromVpd.end(), |
| 603 | l_ccinFromVpd.begin(), ::toupper); |
| 604 | |
| 605 | for (std::string l_ccinValue : i_JsonObject["ccin"]) |
| 606 | { |
| 607 | transform(l_ccinValue.begin(), l_ccinValue.end(), |
| 608 | l_ccinValue.begin(), ::toupper); |
| 609 | |
| 610 | if (l_ccinValue.compare(l_ccinFromVpd) == |
| 611 | constants::STR_CMP_SUCCESS) |
| 612 | { |
| 613 | // CCIN found |
| 614 | l_rc = true; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if (!l_rc) |
| 619 | { |
| 620 | logging::logMessage("No match found for CCIN"); |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 621 | } |
| 622 | } |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 623 | else |
| 624 | { |
| 625 | logging::logMessage("VPD type not supported. Can't find CCIN"); |
| 626 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 627 | } |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 628 | catch (const std::exception& l_ex) |
| 629 | { |
| 630 | const std::string l_errMsg{ |
| 631 | "Failed to find CCIN in VPD. Error : " + std::string(l_ex.what())}; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 632 | |
| Souvik Roy | 815c602 | 2025-02-20 00:54:24 -0600 | [diff] [blame] | 633 | if (typeid(l_ex) == std::type_index(typeid(DataException))) |
| 634 | { |
| 635 | EventLogger::createSyncPel( |
| 636 | types::ErrorType::InvalidVpdMessage, |
| 637 | types::SeverityType::Informational, __FILE__, __FUNCTION__, 0, |
| 638 | l_errMsg, std::nullopt, std::nullopt, std::nullopt, |
| 639 | std::nullopt); |
| 640 | } |
| 641 | |
| 642 | logging::logMessage(l_errMsg); |
| 643 | } |
| 644 | return l_rc; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
| 648 | * @brief API to reset data of a FRU populated under PIM. |
| 649 | * |
| 650 | * This API resets the data for particular interfaces of a FRU under PIM. |
| 651 | * |
| 652 | * @param[in] i_objectPath - DBus object path of the FRU. |
| 653 | * @param[in] io_interfaceMap - Interface and its properties map. |
| Rekha Aparna | 2c04eeb | 2025-10-22 22:15:07 -0500 | [diff] [blame] | 654 | * @param[out] o_errCode - To set error code in case of error. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 655 | */ |
| 656 | inline void resetDataUnderPIM(const std::string& i_objectPath, |
| Rekha Aparna | 2c04eeb | 2025-10-22 22:15:07 -0500 | [diff] [blame] | 657 | types::InterfaceMap& io_interfaceMap, |
| 658 | uint16_t& o_errCode) |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 659 | { |
| Rekha Aparna | 2c04eeb | 2025-10-22 22:15:07 -0500 | [diff] [blame] | 660 | if (i_objectPath.empty()) |
| 661 | { |
| 662 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 663 | return; |
| 664 | } |
| 665 | |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 666 | try |
| 667 | { |
| Anupama B R | 68a7043 | 2025-09-25 02:09:37 -0500 | [diff] [blame] | 668 | std::vector<std::string> l_interfaces; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 669 | const types::MapperGetObject& l_getObjectMap = |
| 670 | dbusUtility::getObjectMap(i_objectPath, l_interfaces); |
| 671 | |
| 672 | const std::vector<std::string>& l_vpdRelatedInterfaces{ |
| 673 | constants::operationalStatusInf, constants::inventoryItemInf, |
| Anupama B R | ffa488f | 2025-05-22 07:34:00 -0500 | [diff] [blame] | 674 | constants::assetInf, constants::vpdCollectionInterface}; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 675 | |
| 676 | for (const auto& [l_service, l_interfaceList] : l_getObjectMap) |
| 677 | { |
| 678 | if (l_service.compare(constants::pimServiceName) != |
| 679 | constants::STR_CMP_SUCCESS) |
| 680 | { |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | for (const auto& l_interface : l_interfaceList) |
| 685 | { |
| 686 | if ((l_interface.find(constants::ipzVpdInf) != |
| 687 | std::string::npos) || |
| 688 | ((std::find(l_vpdRelatedInterfaces.begin(), |
| 689 | l_vpdRelatedInterfaces.end(), l_interface)) != |
| 690 | l_vpdRelatedInterfaces.end())) |
| 691 | { |
| 692 | const types::PropertyMap& l_propertyValueMap = |
| 693 | dbusUtility::getPropertyMap(l_service, i_objectPath, |
| 694 | l_interface); |
| 695 | |
| 696 | types::PropertyMap l_propertyMap; |
| 697 | |
| 698 | for (const auto& l_aProperty : l_propertyValueMap) |
| 699 | { |
| 700 | const std::string& l_propertyName = l_aProperty.first; |
| 701 | const auto& l_propertyValue = l_aProperty.second; |
| 702 | |
| 703 | if (std::holds_alternative<types::BinaryVector>( |
| 704 | l_propertyValue)) |
| 705 | { |
| 706 | l_propertyMap.emplace(l_propertyName, |
| 707 | types::BinaryVector{}); |
| 708 | } |
| 709 | else if (std::holds_alternative<std::string>( |
| 710 | l_propertyValue)) |
| 711 | { |
| Anupama B R | 5cd1b2d | 2025-08-05 04:57:40 -0500 | [diff] [blame] | 712 | if (l_propertyName.compare("Status") == |
| Anupama B R | ffa488f | 2025-05-22 07:34:00 -0500 | [diff] [blame] | 713 | constants::STR_CMP_SUCCESS) |
| 714 | { |
| 715 | l_propertyMap.emplace( |
| 716 | l_propertyName, |
| 717 | constants::vpdCollectionNotStarted); |
| Anupama B R | 4c65fcd | 2025-09-01 08:09:00 -0500 | [diff] [blame] | 718 | l_propertyMap.emplace("StartTime", 0); |
| 719 | l_propertyMap.emplace("CompletedTime", 0); |
| Anupama B R | ffa488f | 2025-05-22 07:34:00 -0500 | [diff] [blame] | 720 | } |
| 721 | else |
| 722 | { |
| 723 | l_propertyMap.emplace(l_propertyName, |
| 724 | std::string{}); |
| 725 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 726 | } |
| 727 | else if (std::holds_alternative<bool>(l_propertyValue)) |
| 728 | { |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 729 | if (l_propertyName.compare("Present") == |
| 730 | constants::STR_CMP_SUCCESS) |
| 731 | { |
| 732 | l_propertyMap.emplace(l_propertyName, false); |
| 733 | } |
| Rekha Aparna | 4f72206 | 2025-05-02 03:39:32 -0500 | [diff] [blame] | 734 | else if (l_propertyName.compare("Functional") == |
| 735 | constants::STR_CMP_SUCCESS) |
| 736 | { |
| 737 | // Since FRU is not present functional property |
| 738 | // is considered as true. |
| 739 | l_propertyMap.emplace(l_propertyName, true); |
| 740 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | io_interfaceMap.emplace(l_interface, |
| 744 | std::move(l_propertyMap)); |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | catch (const std::exception& l_ex) |
| 750 | { |
| Rekha Aparna | 2c04eeb | 2025-10-22 22:15:07 -0500 | [diff] [blame] | 751 | o_errCode = error_code::STANDARD_EXCEPTION; |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 752 | } |
| 753 | } |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 754 | |
| 755 | /** |
| 756 | * @brief API to detect pass1 planar type. |
| 757 | * |
| 758 | * Based on HW version and IM keyword, This API detects is it is a pass1 planar |
| 759 | * or not. |
| 760 | * |
| 761 | * @return True if pass 1 planar, false otherwise. |
| 762 | */ |
| Souvik Roy | 094a735 | 2025-02-20 01:31:45 -0600 | [diff] [blame] | 763 | inline bool isPass1Planar() noexcept |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 764 | { |
| Souvik Roy | 094a735 | 2025-02-20 01:31:45 -0600 | [diff] [blame] | 765 | bool l_rc{false}; |
| 766 | try |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 767 | { |
| Souvik Roy | 094a735 | 2025-02-20 01:31:45 -0600 | [diff] [blame] | 768 | auto l_retVal = dbusUtility::readDbusProperty( |
| 769 | constants::pimServiceName, constants::systemVpdInvPath, |
| 770 | constants::viniInf, constants::kwdHW); |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 771 | |
| Souvik Roy | 094a735 | 2025-02-20 01:31:45 -0600 | [diff] [blame] | 772 | auto l_hwVer = std::get_if<types::BinaryVector>(&l_retVal); |
| 773 | |
| 774 | l_retVal = dbusUtility::readDbusProperty( |
| 775 | constants::pimServiceName, constants::systemInvPath, |
| 776 | constants::vsbpInf, constants::kwdIM); |
| 777 | |
| 778 | auto l_imValue = std::get_if<types::BinaryVector>(&l_retVal); |
| 779 | |
| 780 | if (l_hwVer && l_imValue) |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 781 | { |
| Souvik Roy | 094a735 | 2025-02-20 01:31:45 -0600 | [diff] [blame] | 782 | if (l_hwVer->size() != constants::VALUE_2) |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 783 | { |
| Souvik Roy | 094a735 | 2025-02-20 01:31:45 -0600 | [diff] [blame] | 784 | throw std::runtime_error("Invalid HW keyword length."); |
| 785 | } |
| 786 | |
| 787 | if (l_imValue->size() != constants::VALUE_4) |
| 788 | { |
| 789 | throw std::runtime_error("Invalid IM keyword length."); |
| 790 | } |
| 791 | |
| 792 | const types::BinaryVector l_everest{80, 00, 48, 00}; |
| 793 | const types::BinaryVector l_fuji{96, 00, 32, 00}; |
| 794 | |
| 795 | if (((*l_imValue) == l_everest) || ((*l_imValue) == l_fuji)) |
| 796 | { |
| 797 | if ((*l_hwVer).at(1) < constants::VALUE_21) |
| 798 | { |
| 799 | l_rc = true; |
| 800 | } |
| 801 | } |
| 802 | else if ((*l_hwVer).at(1) < constants::VALUE_2) |
| 803 | { |
| 804 | l_rc = true; |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 805 | } |
| 806 | } |
| Souvik Roy | 094a735 | 2025-02-20 01:31:45 -0600 | [diff] [blame] | 807 | } |
| 808 | catch (const std::exception& l_ex) |
| 809 | { |
| 810 | logging::logMessage("Failed to check for pass 1 planar. Error: " + |
| 811 | std::string(l_ex.what())); |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 812 | } |
| 813 | |
| Souvik Roy | 094a735 | 2025-02-20 01:31:45 -0600 | [diff] [blame] | 814 | return l_rc; |
| Sunny Srivastava | 78c9107 | 2025-02-05 14:09:50 +0530 | [diff] [blame] | 815 | } |
| Sunny Srivastava | c6ef42d | 2025-02-19 19:17:10 +0530 | [diff] [blame] | 816 | |
| 817 | /** |
| 818 | * @brief API to detect if system configuration is that of PowerVS system. |
| 819 | * |
| 820 | * @param[in] i_imValue - IM value of the system. |
| Rekha Aparna | ed09af8 | 2025-10-22 20:34:41 -0500 | [diff] [blame] | 821 | * @param[out] o_errCode - To set error code in case of error. |
| Sunny Srivastava | c6ef42d | 2025-02-19 19:17:10 +0530 | [diff] [blame] | 822 | * @return true if it is PowerVS configuration, false otherwise. |
| 823 | */ |
| Rekha Aparna | ed09af8 | 2025-10-22 20:34:41 -0500 | [diff] [blame] | 824 | inline bool isPowerVsConfiguration(const types::BinaryVector& i_imValue, |
| 825 | uint16_t& o_errCode) |
| Sunny Srivastava | c6ef42d | 2025-02-19 19:17:10 +0530 | [diff] [blame] | 826 | { |
| 827 | if (i_imValue.empty() || i_imValue.size() != constants::VALUE_4) |
| 828 | { |
| Rekha Aparna | ed09af8 | 2025-10-22 20:34:41 -0500 | [diff] [blame] | 829 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| Sunny Srivastava | c6ef42d | 2025-02-19 19:17:10 +0530 | [diff] [blame] | 830 | return false; |
| 831 | } |
| 832 | |
| 833 | // Should be a 0x5000XX series system. |
| 834 | if (i_imValue.at(0) == constants::HEX_VALUE_50 && |
| 835 | i_imValue.at(1) == constants::HEX_VALUE_00) |
| 836 | { |
| 837 | std::string l_imagePrefix = dbusUtility::getImagePrefix(); |
| 838 | |
| 839 | // Check image for 0x500030XX series. |
| 840 | if ((i_imValue.at(2) == constants::HEX_VALUE_30) && |
| 841 | ((l_imagePrefix == constants::powerVsImagePrefix_MY) || |
| 842 | (l_imagePrefix == constants::powerVsImagePrefix_NY))) |
| 843 | { |
| 844 | logging::logMessage("PowerVS configuration"); |
| 845 | return true; |
| 846 | } |
| 847 | |
| 848 | // Check image for 0X500010XX series. |
| 849 | if ((i_imValue.at(2) == constants::HEX_VALUE_10) && |
| 850 | ((l_imagePrefix == constants::powerVsImagePrefix_MZ) || |
| 851 | (l_imagePrefix == constants::powerVsImagePrefix_NZ))) |
| 852 | { |
| 853 | logging::logMessage("PowerVS configuration"); |
| 854 | return true; |
| 855 | } |
| 856 | } |
| 857 | return false; |
| 858 | } |
| Sunny Srivastava | f1dda76 | 2025-02-19 23:46:19 +0530 | [diff] [blame] | 859 | |
| 860 | /** |
| 861 | * @brief API to get CCIN for a given FRU from DBus. |
| 862 | * |
| 863 | * The API reads the CCIN for a FRU based on its inventory path. |
| 864 | * |
| 865 | * @param[in] i_invObjPath - Inventory path of the FRU. |
| 866 | * @return CCIN of the FRU on success, empty string otherwise. |
| 867 | */ |
| 868 | inline std::string getCcinFromDbus(const std::string& i_invObjPath) |
| 869 | { |
| 870 | try |
| 871 | { |
| 872 | if (i_invObjPath.empty()) |
| 873 | { |
| 874 | throw std::runtime_error("Empty EEPROM path, can't read CCIN"); |
| 875 | } |
| 876 | |
| 877 | const auto& l_retValue = dbusUtility::readDbusProperty( |
| 878 | constants::pimServiceName, i_invObjPath, constants::viniInf, |
| 879 | constants::kwdCCIN); |
| 880 | |
| 881 | auto l_ptrCcin = std::get_if<types::BinaryVector>(&l_retValue); |
| 882 | if (!l_ptrCcin || (*l_ptrCcin).size() != constants::VALUE_4) |
| 883 | { |
| 884 | throw DbusException("Invalid CCIN read from Dbus"); |
| 885 | } |
| 886 | |
| 887 | return std::string((*l_ptrCcin).begin(), (*l_ptrCcin).end()); |
| 888 | } |
| 889 | catch (const std::exception& l_ex) |
| 890 | { |
| 891 | logging::logMessage(l_ex.what()); |
| 892 | return std::string{}; |
| 893 | } |
| 894 | } |
| Sunny Srivastava | ecfaa21 | 2025-03-24 13:02:13 +0530 | [diff] [blame] | 895 | |
| 896 | /** |
| 897 | * @brief API to check if the current running image is a powerVS image. |
| 898 | * |
| 899 | * @return true if it is PowerVS image, false otherwise. |
| 900 | */ |
| 901 | inline bool isPowerVsImage() |
| 902 | { |
| 903 | std::string l_imagePrefix = dbusUtility::getImagePrefix(); |
| 904 | |
| 905 | if ((l_imagePrefix == constants::powerVsImagePrefix_MY) || |
| 906 | (l_imagePrefix == constants::powerVsImagePrefix_NY) || |
| 907 | (l_imagePrefix == constants::powerVsImagePrefix_MZ) || |
| 908 | (l_imagePrefix == constants::powerVsImagePrefix_NZ)) |
| 909 | { |
| 910 | return true; |
| 911 | } |
| 912 | return false; |
| 913 | } |
| Souvik Roy | 2ee8a21 | 2025-04-24 02:37:59 -0500 | [diff] [blame] | 914 | |
| 915 | /** |
| 916 | * @brief API to sync keyword update to inherited FRUs. |
| 917 | * |
| 918 | * For a given keyword update on a EEPROM path, this API syncs the keyword |
| 919 | * update to all inherited FRUs' respective interface, property on PIM. |
| 920 | * |
| 921 | * @param[in] i_fruPath - EEPROM path of FRU. |
| 922 | * @param[in] i_paramsToWriteData - Input details. |
| 923 | * @param[in] i_sysCfgJsonObj - System config JSON. |
| 924 | * |
| 925 | */ |
| 926 | inline void updateKwdOnInheritedFrus( |
| 927 | const std::string& i_fruPath, |
| 928 | const types::WriteVpdParams& i_paramsToWriteData, |
| 929 | const nlohmann::json& i_sysCfgJsonObj) noexcept |
| 930 | { |
| 931 | try |
| 932 | { |
| 933 | if (!i_sysCfgJsonObj.contains("frus")) |
| 934 | { |
| 935 | throw std::runtime_error("Mandatory tag(s) missing from JSON"); |
| 936 | } |
| 937 | |
| 938 | if (!i_sysCfgJsonObj["frus"].contains(i_fruPath)) |
| 939 | { |
| 940 | throw std::runtime_error( |
| 941 | "VPD path [" + i_fruPath + "] not found in system config JSON"); |
| 942 | } |
| 943 | |
| 944 | const types::IpzData* l_ipzData = |
| 945 | std::get_if<types::IpzData>(&i_paramsToWriteData); |
| 946 | |
| 947 | if (!l_ipzData) |
| 948 | { |
| 949 | throw std::runtime_error("Unsupported VPD type"); |
| 950 | } |
| 951 | // iterate through all inventory paths for given EEPROM path, |
| 952 | // except the base FRU. |
| 953 | // if for an inventory path, "inherit" tag is true, |
| 954 | // update the inventory path's com.ibm.ipzvpd.<record>,keyword |
| 955 | // property |
| 956 | |
| 957 | types::ObjectMap l_objectInterfaceMap; |
| 958 | |
| 959 | auto l_populateInterfaceMap = |
| 960 | [&l_objectInterfaceMap, |
| 961 | &l_ipzData = std::as_const(l_ipzData)](const auto& l_Fru) { |
| 962 | // update inherited FRUs only |
| 963 | if (l_Fru.value("inherit", true)) |
| 964 | { |
| 965 | l_objectInterfaceMap.emplace( |
| 966 | sdbusplus::message::object_path{l_Fru["inventoryPath"]}, |
| 967 | types::InterfaceMap{ |
| 968 | {std::string{constants::ipzVpdInf + |
| 969 | std::get<0>(*l_ipzData)}, |
| 970 | types::PropertyMap{{std::get<1>(*l_ipzData), |
| 971 | std::get<2>(*l_ipzData)}}}}); |
| 972 | } |
| 973 | }; |
| 974 | |
| 975 | // iterate through all FRUs except the base FRU |
| 976 | std::for_each( |
| 977 | i_sysCfgJsonObj["frus"][i_fruPath].begin() + constants::VALUE_1, |
| 978 | i_sysCfgJsonObj["frus"][i_fruPath].end(), l_populateInterfaceMap); |
| 979 | |
| 980 | if (!l_objectInterfaceMap.empty()) |
| 981 | { |
| 982 | // notify PIM |
| 983 | if (!dbusUtility::callPIM(move(l_objectInterfaceMap))) |
| 984 | { |
| 985 | throw std::runtime_error( |
| 986 | "Call to PIM failed for VPD file " + i_fruPath); |
| 987 | } |
| 988 | } |
| 989 | } |
| 990 | catch (const std::exception& l_ex) |
| 991 | { |
| 992 | logging::logMessage( |
| 993 | "Failed to sync keyword update to inherited FRUs of FRU [" + |
| 994 | i_fruPath + "]. Error: " + std::string(l_ex.what())); |
| 995 | } |
| 996 | } |
| Souvik Roy | 96ebe96 | 2025-04-29 04:01:07 -0500 | [diff] [blame] | 997 | |
| 998 | /** |
| 999 | * @brief API to get common interface(s) properties corresponding to given |
| 1000 | * record and keyword. |
| 1001 | * |
| 1002 | * For a given record and keyword, this API finds the corresponding common |
| 1003 | * interfaces(s) properties from the system config JSON and populates an |
| 1004 | * interface map with the respective properties and values. |
| 1005 | * |
| 1006 | * @param[in] i_paramsToWriteData - Input details. |
| 1007 | * @param[in] i_commonInterfaceJson - Common interface JSON object. |
| 1008 | * |
| 1009 | * @return Returns a map of common interface(s) and properties corresponding to |
| 1010 | * the record and keyword. An empty map is returned if no such common |
| 1011 | * interface(s) and properties are found. |
| 1012 | */ |
| 1013 | inline types::InterfaceMap getCommonInterfaceProperties( |
| 1014 | const types::WriteVpdParams& i_paramsToWriteData, |
| 1015 | const nlohmann::json& i_commonInterfaceJson) noexcept |
| 1016 | { |
| 1017 | types::InterfaceMap l_interfaceMap; |
| 1018 | try |
| 1019 | { |
| 1020 | const types::IpzData* l_ipzData = |
| 1021 | std::get_if<types::IpzData>(&i_paramsToWriteData); |
| 1022 | |
| 1023 | if (!l_ipzData) |
| 1024 | { |
| 1025 | throw std::runtime_error("Invalid VPD type"); |
| 1026 | } |
| 1027 | |
| 1028 | auto l_populateInterfaceMap = [&l_ipzData = std::as_const(l_ipzData), |
| 1029 | &l_interfaceMap]( |
| 1030 | const auto& l_interfacesPropPair) { |
| Souvik Roy | 9654e03 | 2025-09-17 17:18:42 +0000 | [diff] [blame] | 1031 | if (l_interfacesPropPair.value().empty()) |
| 1032 | { |
| 1033 | return; |
| 1034 | } |
| 1035 | |
| Souvik Roy | 96ebe96 | 2025-04-29 04:01:07 -0500 | [diff] [blame] | 1036 | // find matching property value pair |
| 1037 | const auto l_matchPropValuePairIt = std::find_if( |
| 1038 | l_interfacesPropPair.value().items().begin(), |
| 1039 | l_interfacesPropPair.value().items().end(), |
| 1040 | [&l_ipzData](const auto& l_propValuePair) { |
| 1041 | return (l_propValuePair.value().value("recordName", "") == |
| 1042 | std::get<0>(*l_ipzData) && |
| 1043 | l_propValuePair.value().value("keywordName", "") == |
| 1044 | std::get<1>(*l_ipzData)); |
| 1045 | }); |
| 1046 | |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 1047 | uint16_t l_errCode = 0; |
| 1048 | |
| Souvik Roy | 96ebe96 | 2025-04-29 04:01:07 -0500 | [diff] [blame] | 1049 | if (l_matchPropValuePairIt != |
| 1050 | l_interfacesPropPair.value().items().end()) |
| 1051 | { |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 1052 | std::string l_kwd = std::string(std::get<2>(*l_ipzData).begin(), |
| 1053 | std::get<2>(*l_ipzData).end()); |
| 1054 | |
| 1055 | std::string l_encodedValue = vpdSpecificUtility::encodeKeyword( |
| 1056 | l_kwd, l_matchPropValuePairIt.value().value("encoding", ""), |
| 1057 | l_errCode); |
| 1058 | |
| 1059 | if (l_errCode) |
| 1060 | { |
| 1061 | logging::logMessage( |
| 1062 | "Failed to get encoded value for key : " + l_kwd + |
| 1063 | " ,error : " + commonUtility::getErrCodeMsg(l_errCode)); |
| 1064 | } |
| 1065 | |
| Souvik Roy | 96ebe96 | 2025-04-29 04:01:07 -0500 | [diff] [blame] | 1066 | // add property map to interface map |
| 1067 | l_interfaceMap.emplace( |
| 1068 | l_interfacesPropPair.key(), |
| 1069 | types::PropertyMap{ |
| Rekha Aparna | 17ddfb5 | 2025-10-09 20:45:53 -0500 | [diff] [blame] | 1070 | {l_matchPropValuePairIt.key(), l_encodedValue}}); |
| Souvik Roy | 96ebe96 | 2025-04-29 04:01:07 -0500 | [diff] [blame] | 1071 | } |
| 1072 | }; |
| 1073 | |
| Sunny Srivastava | 89fbd2d | 2025-09-03 02:03:48 -0500 | [diff] [blame] | 1074 | if (!i_commonInterfaceJson.empty()) |
| 1075 | { |
| 1076 | // iterate through all common interfaces and populate interface map |
| 1077 | std::for_each(i_commonInterfaceJson.items().begin(), |
| 1078 | i_commonInterfaceJson.items().end(), |
| 1079 | l_populateInterfaceMap); |
| 1080 | } |
| Souvik Roy | 96ebe96 | 2025-04-29 04:01:07 -0500 | [diff] [blame] | 1081 | } |
| 1082 | catch (const std::exception& l_ex) |
| 1083 | { |
| 1084 | logging::logMessage( |
| 1085 | "Failed to find common interface properties. Error: " + |
| 1086 | std::string(l_ex.what())); |
| 1087 | } |
| 1088 | return l_interfaceMap; |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * @brief API to update common interface(s) properties when keyword is updated. |
| 1093 | * |
| 1094 | * For a given keyword update on a EEPROM path, this API syncs the keyword |
| 1095 | * update to respective common interface(s) properties of the base FRU and all |
| 1096 | * inherited FRUs. |
| 1097 | * |
| 1098 | * @param[in] i_fruPath - EEPROM path of FRU. |
| 1099 | * @param[in] i_paramsToWriteData - Input details. |
| 1100 | * @param[in] i_sysCfgJsonObj - System config JSON. |
| 1101 | * |
| 1102 | */ |
| 1103 | inline void updateCiPropertyOfInheritedFrus( |
| 1104 | const std::string& i_fruPath, |
| 1105 | const types::WriteVpdParams& i_paramsToWriteData, |
| 1106 | const nlohmann::json& i_sysCfgJsonObj) noexcept |
| 1107 | { |
| 1108 | try |
| 1109 | { |
| 1110 | if (!i_sysCfgJsonObj.contains("commonInterfaces")) |
| 1111 | { |
| 1112 | // no common interfaces in JSON, nothing to do |
| 1113 | return; |
| 1114 | } |
| 1115 | |
| 1116 | if (!i_sysCfgJsonObj.contains("frus")) |
| 1117 | { |
| 1118 | throw std::runtime_error("Mandatory tag(s) missing from JSON"); |
| 1119 | } |
| 1120 | |
| 1121 | if (!i_sysCfgJsonObj["frus"].contains(i_fruPath)) |
| 1122 | { |
| 1123 | throw std::runtime_error( |
| 1124 | "VPD path [" + i_fruPath + "] not found in system config JSON"); |
| 1125 | } |
| 1126 | |
| 1127 | if (!std::get_if<types::IpzData>(&i_paramsToWriteData)) |
| 1128 | { |
| 1129 | throw std::runtime_error("Unsupported VPD type"); |
| 1130 | } |
| 1131 | |
| 1132 | // iterate through all inventory paths for given EEPROM path, |
| 1133 | // if for an inventory path, "inherit" tag is true, |
| 1134 | // update the inventory path's com.ibm.ipzvpd.<record>,keyword |
| 1135 | // property |
| 1136 | |
| 1137 | types::ObjectMap l_objectInterfaceMap; |
| 1138 | |
| 1139 | const types::InterfaceMap l_interfaceMap = getCommonInterfaceProperties( |
| 1140 | i_paramsToWriteData, i_sysCfgJsonObj["commonInterfaces"]); |
| 1141 | |
| 1142 | if (l_interfaceMap.empty()) |
| 1143 | { |
| 1144 | // nothing to do |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | auto l_populateObjectInterfaceMap = |
| 1149 | [&l_objectInterfaceMap, &l_interfaceMap = std::as_const( |
| 1150 | l_interfaceMap)](const auto& l_Fru) { |
| 1151 | if (l_Fru.value("inherit", true) && |
| 1152 | l_Fru.contains("inventoryPath")) |
| 1153 | { |
| 1154 | l_objectInterfaceMap.emplace( |
| 1155 | sdbusplus::message::object_path{l_Fru["inventoryPath"]}, |
| 1156 | l_interfaceMap); |
| 1157 | } |
| 1158 | }; |
| 1159 | |
| 1160 | std::for_each(i_sysCfgJsonObj["frus"][i_fruPath].begin(), |
| 1161 | i_sysCfgJsonObj["frus"][i_fruPath].end(), |
| 1162 | l_populateObjectInterfaceMap); |
| 1163 | |
| 1164 | if (!l_objectInterfaceMap.empty()) |
| 1165 | { |
| 1166 | // notify PIM |
| 1167 | if (!dbusUtility::callPIM(move(l_objectInterfaceMap))) |
| 1168 | { |
| 1169 | throw std::runtime_error( |
| 1170 | "Call to PIM failed for VPD file " + i_fruPath); |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | catch (const std::exception& l_ex) |
| 1175 | { |
| 1176 | logging::logMessage( |
| 1177 | "Failed to update common interface properties of FRU [" + |
| 1178 | i_fruPath + "]. Error: " + std::string(l_ex.what())); |
| 1179 | } |
| 1180 | } |
| Sunny Srivastava | 995e1c2 | 2025-08-28 03:13:00 -0500 | [diff] [blame] | 1181 | |
| 1182 | /** |
| Souvik Roy | d7c7cb5 | 2025-07-30 06:31:02 +0000 | [diff] [blame] | 1183 | * @brief API to convert write VPD parameters to a string. |
| 1184 | * |
| 1185 | * @param[in] i_paramsToWriteData - write VPD parameters. |
| 1186 | * @param[out] o_errCode - To set error code in case of error. |
| 1187 | * |
| 1188 | * @return On success returns string representation of write VPD parameters, |
| 1189 | * otherwise returns an empty string. |
| 1190 | */ |
| 1191 | inline const std::string convertWriteVpdParamsToString( |
| 1192 | const types::WriteVpdParams& i_paramsToWriteData, |
| 1193 | uint16_t& o_errCode) noexcept |
| 1194 | { |
| 1195 | try |
| 1196 | { |
| 1197 | if (const types::IpzData* l_ipzDataPtr = |
| 1198 | std::get_if<types::IpzData>(&i_paramsToWriteData)) |
| 1199 | { |
| 1200 | return std::string{ |
| 1201 | "Record: " + std::get<0>(*l_ipzDataPtr) + |
| 1202 | " Keyword: " + std::get<1>(*l_ipzDataPtr) + " Value: " + |
| 1203 | commonUtility::convertByteVectorToHex( |
| 1204 | std::get<2>(*l_ipzDataPtr))}; |
| 1205 | } |
| 1206 | else if (const types::KwData* l_kwDataPtr = |
| 1207 | std::get_if<types::KwData>(&i_paramsToWriteData)) |
| 1208 | { |
| 1209 | return std::string{ |
| 1210 | "Keyword: " + std::get<0>(*l_kwDataPtr) + " Value: " + |
| 1211 | commonUtility::convertByteVectorToHex( |
| 1212 | std::get<1>(*l_kwDataPtr))}; |
| 1213 | } |
| 1214 | else |
| 1215 | { |
| 1216 | o_errCode = error_code::UNSUPPORTED_VPD_TYPE; |
| 1217 | } |
| 1218 | } |
| 1219 | catch (const std::exception& l_ex) |
| 1220 | { |
| 1221 | o_errCode = error_code::STANDARD_EXCEPTION; |
| 1222 | } |
| 1223 | return std::string{}; |
| 1224 | } |
| 1225 | |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1226 | } // namespace vpdSpecificUtility |
| 1227 | } // namespace vpd |