Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 3 | #include "error_codes.hpp" |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 4 | #include "event_logger.hpp" |
| 5 | #include "exceptions.hpp" |
| 6 | #include "logger.hpp" |
| 7 | #include "types.hpp" |
| 8 | |
| 9 | #include <gpiod.hpp> |
| 10 | #include <nlohmann/json.hpp> |
| 11 | #include <utility/common_utility.hpp> |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 12 | #include <utility/vpd_specific_utility.hpp> |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 13 | |
| 14 | #include <fstream> |
| 15 | #include <type_traits> |
| 16 | #include <unordered_map> |
| 17 | |
| 18 | namespace vpd |
| 19 | { |
| 20 | namespace jsonUtility |
| 21 | { |
| 22 | |
| 23 | // forward declaration of API for function map. |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 24 | bool processSystemCmdTag( |
| 25 | const nlohmann::json& i_parsedConfigJson, const std::string& i_vpdFilePath, |
| 26 | const std::string& i_baseAction, const std::string& i_flagToProcess, |
| 27 | uint16_t& o_errCode); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 28 | |
| 29 | // forward declaration of API for function map. |
| 30 | bool processGpioPresenceTag( |
| 31 | const nlohmann::json& i_parsedConfigJson, const std::string& i_vpdFilePath, |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 32 | const std::string& i_baseAction, const std::string& i_flagToProcess, |
| 33 | uint16_t& o_errCode); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 34 | |
| 35 | // forward declaration of API for function map. |
| 36 | bool procesSetGpioTag(const nlohmann::json& i_parsedConfigJson, |
| 37 | const std::string& i_vpdFilePath, |
| 38 | const std::string& i_baseAction, |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 39 | const std::string& i_flagToProcess, uint16_t& o_errCode); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 40 | |
| 41 | // Function pointers to process tags from config JSON. |
| 42 | typedef bool (*functionPtr)( |
| 43 | const nlohmann::json& i_parsedConfigJson, const std::string& i_vpdFilePath, |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 44 | const std::string& i_baseAction, const std::string& i_flagToProcess, |
| 45 | uint16_t& o_errCode); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 46 | |
| 47 | inline std::unordered_map<std::string, functionPtr> funcionMap{ |
| 48 | {"gpioPresence", processGpioPresenceTag}, |
| 49 | {"setGpio", procesSetGpioTag}, |
| 50 | {"systemCmd", processSystemCmdTag}}; |
| 51 | |
| 52 | /** |
| 53 | * @brief API to read VPD offset from JSON file. |
| 54 | * |
| 55 | * @param[in] i_sysCfgJsonObj - Parsed system config JSON object. |
| 56 | * @param[in] i_vpdFilePath - VPD file path. |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 57 | * @param[in] o_errCode - To set error code in case of error. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 58 | * @return VPD offset if found in JSON, 0 otherwise. |
| 59 | */ |
| 60 | inline size_t getVPDOffset(const nlohmann::json& i_sysCfgJsonObj, |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 61 | const std::string& i_vpdFilePath, |
| 62 | uint16_t& o_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 63 | { |
| 64 | if (i_vpdFilePath.empty() || (i_sysCfgJsonObj.empty()) || |
| 65 | (!i_sysCfgJsonObj.contains("frus"))) |
| 66 | { |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 67 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | if (i_sysCfgJsonObj["frus"].contains(i_vpdFilePath)) |
| 72 | { |
| 73 | return i_sysCfgJsonObj["frus"][i_vpdFilePath].at(0).value("offset", 0); |
| 74 | } |
| 75 | |
| 76 | const nlohmann::json& l_fruList = |
| 77 | i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 78 | |
| 79 | for (const auto& l_fru : l_fruList.items()) |
| 80 | { |
| 81 | const auto l_fruPath = l_fru.key(); |
| 82 | |
| 83 | // check if given path is redundant FRU path |
| 84 | if (i_vpdFilePath == i_sysCfgJsonObj["frus"][l_fruPath].at(0).value( |
| 85 | "redundantEeprom", "")) |
| 86 | { |
| 87 | // Return the offset of redundant EEPROM taken from JSON. |
| 88 | return i_sysCfgJsonObj["frus"][l_fruPath].at(0).value("offset", 0); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @brief API to parse respective JSON. |
| 97 | * |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 98 | * @param[in] pathToJson - Path to JSON. |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 99 | * @param[out] o_errCode - To set error code in case of error. |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 100 | * @return on success parsed JSON. On failure empty JSON object. |
| 101 | * |
| 102 | * Note: Caller has to handle it in case an empty JSON object is received. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 103 | */ |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 104 | inline nlohmann::json getParsedJson(const std::string& pathToJson, |
| 105 | uint16_t& o_errCode) noexcept |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 106 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 107 | if (pathToJson.empty()) |
| 108 | { |
| 109 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 110 | return nlohmann::json{}; |
| 111 | } |
| 112 | |
| 113 | if (!std::filesystem::exists(pathToJson)) |
| 114 | { |
| 115 | o_errCode = error_code::FILE_NOT_FOUND; |
| 116 | return nlohmann::json{}; |
| 117 | } |
| 118 | |
| 119 | if (std::filesystem::is_empty(pathToJson)) |
| 120 | { |
| 121 | o_errCode = error_code::EMPTY_FILE; |
| 122 | return nlohmann::json{}; |
| 123 | } |
| 124 | |
| 125 | std::ifstream l_jsonFile(pathToJson); |
| 126 | if (!l_jsonFile) |
| 127 | { |
| 128 | o_errCode = error_code::FILE_ACCESS_ERROR; |
| 129 | return nlohmann::json{}; |
| 130 | } |
| 131 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 132 | try |
| 133 | { |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 134 | return nlohmann::json::parse(l_jsonFile); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 135 | } |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 136 | catch (const std::exception& l_ex) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 137 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 138 | o_errCode = error_code::JSON_PARSE_ERROR; |
| 139 | return nlohmann::json{}; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * @brief Get inventory object path from system config JSON. |
| 145 | * |
| 146 | * Given either D-bus inventory path/FRU EEPROM path/redundant EEPROM path, |
| 147 | * this API returns D-bus inventory path if present in JSON. |
| 148 | * |
| 149 | * @param[in] i_sysCfgJsonObj - System config JSON object |
| 150 | * @param[in] i_vpdPath - Path to where VPD is stored. |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 151 | * @param[in] o_errCode - To set error code in case of error. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 152 | * |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 153 | * @return On success a valid path is returned, on failure an empty string is |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 154 | * returned. |
| 155 | * |
| 156 | * Note: Caller has to handle it in case an empty string is received. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 157 | */ |
| 158 | inline std::string getInventoryObjPathFromJson( |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 159 | const nlohmann::json& i_sysCfgJsonObj, const std::string& i_vpdPath, |
| 160 | uint16_t& o_errCode) noexcept |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 161 | { |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 162 | if (i_vpdPath.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 163 | { |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 164 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 165 | return std::string{}; |
RekhaAparna01 | 1ef2100 | 2025-02-18 23:47:36 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 168 | if (!i_sysCfgJsonObj.contains("frus")) |
| 169 | { |
| 170 | o_errCode = error_code::INVALID_JSON; |
| 171 | return std::string{}; |
| 172 | } |
| 173 | |
| 174 | // check if given path is FRU path |
| 175 | if (i_sysCfgJsonObj["frus"].contains(i_vpdPath)) |
| 176 | { |
| 177 | return i_sysCfgJsonObj["frus"][i_vpdPath].at(0).value( |
| 178 | "inventoryPath", ""); |
| 179 | } |
| 180 | |
| 181 | const nlohmann::json& l_fruList = |
| 182 | i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 183 | |
| 184 | for (const auto& l_fru : l_fruList.items()) |
| 185 | { |
| 186 | const auto l_fruPath = l_fru.key(); |
| 187 | const auto l_invObjPath = |
| 188 | i_sysCfgJsonObj["frus"][l_fruPath].at(0).value("inventoryPath", ""); |
| 189 | |
| 190 | // check if given path is redundant FRU path or inventory path |
| 191 | if (i_vpdPath == i_sysCfgJsonObj["frus"][l_fruPath].at(0).value( |
| 192 | "redundantEeprom", "") || |
| 193 | (i_vpdPath == l_invObjPath)) |
| 194 | { |
| 195 | return l_invObjPath; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return std::string{}; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @brief Process "PostFailAction" defined in config JSON. |
| 204 | * |
| 205 | * In case there is some error in the processing of "preAction" execution and a |
| 206 | * set of procedure needs to be done as a part of post fail action. This base |
| 207 | * action can be defined in the config JSON for that FRU and it will be handled |
| 208 | * under this API. |
| 209 | * |
| 210 | * @param[in] i_parsedConfigJson - config JSON |
| 211 | * @param[in] i_vpdFilePath - EEPROM file path |
| 212 | * @param[in] i_flagToProcess - To identify which flag(s) needs to be processed |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 213 | * @param[out] o_errCode - To set error code in case of error |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 214 | * under PostFailAction tag of config JSON. |
| 215 | * @return - success or failure |
| 216 | */ |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 217 | inline bool executePostFailAction( |
| 218 | const nlohmann::json& i_parsedConfigJson, const std::string& i_vpdFilePath, |
| 219 | const std::string& i_flagToProcess, uint16_t& o_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 220 | { |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 221 | if (i_parsedConfigJson.empty() || i_vpdFilePath.empty() || |
| 222 | i_flagToProcess.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 223 | { |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 224 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 225 | return false; |
| 226 | } |
RekhaAparna01 | c11e8b6 | 2025-02-20 00:34:35 -0600 | [diff] [blame] | 227 | |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 228 | if (!(i_parsedConfigJson["frus"][i_vpdFilePath].at(0))["postFailAction"] |
| 229 | .contains(i_flagToProcess)) |
| 230 | { |
| 231 | o_errCode = error_code::MISSING_FLAG; |
| 232 | return false; |
| 233 | } |
RekhaAparna01 | c11e8b6 | 2025-02-20 00:34:35 -0600 | [diff] [blame] | 234 | |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 235 | for (const auto& l_tags : (i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 236 | 0))["postFailAction"][i_flagToProcess] |
| 237 | .items()) |
| 238 | { |
| 239 | auto itrToFunction = funcionMap.find(l_tags.key()); |
| 240 | if (itrToFunction != funcionMap.end()) |
RekhaAparna01 | c11e8b6 | 2025-02-20 00:34:35 -0600 | [diff] [blame] | 241 | { |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 242 | if (!itrToFunction->second(i_parsedConfigJson, i_vpdFilePath, |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 243 | "postFailAction", i_flagToProcess, |
| 244 | o_errCode)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 245 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 246 | if (o_errCode) |
| 247 | { |
| 248 | logging::logMessage( |
| 249 | l_tags.key() + " failed for [" + i_vpdFilePath + |
| 250 | "]. Reason " + |
| 251 | vpdSpecificUtility::getErrCodeMsg(o_errCode)); |
| 252 | } |
Rekha Aparna | ff7d799 | 2025-09-01 11:08:53 -0500 | [diff] [blame] | 253 | return false; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * @brief Process "systemCmd" tag for a given FRU. |
| 263 | * |
| 264 | * The API will process "systemCmd" tag if it is defined in the config |
| 265 | * JSON for the given FRU. |
| 266 | * |
| 267 | * @param[in] i_parsedConfigJson - config JSON |
| 268 | * @param[in] i_vpdFilePath - EEPROM file path |
| 269 | * @param[in] i_baseAction - Base action for which this tag has been called. |
| 270 | * @param[in] i_flagToProcess - Flag nested under the base action for which this |
| 271 | * tag has been called. |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 272 | * @param[out] o_errCode - To set error code in case of error. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 273 | * @return Execution status. |
| 274 | */ |
| 275 | inline bool processSystemCmdTag( |
| 276 | const nlohmann::json& i_parsedConfigJson, const std::string& i_vpdFilePath, |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 277 | const std::string& i_baseAction, const std::string& i_flagToProcess, |
| 278 | uint16_t& o_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 279 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 280 | if (i_vpdFilePath.empty() || i_parsedConfigJson.empty() || |
| 281 | i_baseAction.empty() || i_flagToProcess.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 282 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 283 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 284 | return false; |
| 285 | } |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 286 | |
| 287 | if (!((i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 288 | 0)[i_baseAction][i_flagToProcess]["systemCmd"]) |
| 289 | .contains("cmd"))) |
| 290 | { |
| 291 | o_errCode = error_code::MISSING_FLAG; |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | const std::string& l_systemCommand = |
| 296 | i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 297 | 0)[i_baseAction][i_flagToProcess]["systemCmd"]["cmd"]; |
| 298 | |
| 299 | commonUtility::executeCmd(l_systemCommand); |
| 300 | return true; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @brief Checks for presence of a given FRU using GPIO line. |
| 305 | * |
| 306 | * This API returns the presence information of the FRU corresponding to the |
| 307 | * given VPD file path by setting the presence pin. |
| 308 | * |
| 309 | * @param[in] i_parsedConfigJson - config JSON |
| 310 | * @param[in] i_vpdFilePath - EEPROM file path |
| 311 | * @param[in] i_baseAction - Base action for which this tag has been called. |
| 312 | * @param[in] i_flagToProcess - Flag nested under the base action for which this |
| 313 | * tag has been called. |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 314 | * @param[out] o_errCode - To set error code in case of error |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 315 | * @return Execution status. |
| 316 | */ |
| 317 | inline bool processGpioPresenceTag( |
| 318 | const nlohmann::json& i_parsedConfigJson, const std::string& i_vpdFilePath, |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 319 | const std::string& i_baseAction, const std::string& i_flagToProcess, |
| 320 | uint16_t& o_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 321 | { |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 322 | std::string l_presencePinName; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 323 | try |
| 324 | { |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 325 | if (i_vpdFilePath.empty() || i_parsedConfigJson.empty() || |
| 326 | i_baseAction.empty() || i_flagToProcess.empty()) |
| 327 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 328 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 329 | return false; |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if (!(((i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 333 | 0)[i_baseAction][i_flagToProcess]["gpioPresence"]) |
| 334 | .contains("pin")) && |
| 335 | ((i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 336 | 0)[i_baseAction][i_flagToProcess]["gpioPresence"]) |
| 337 | .contains("value")))) |
| 338 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 339 | o_errCode = error_code::JSON_MISSING_GPIO_INFO; |
| 340 | return false; |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // get the pin name |
| 344 | l_presencePinName = i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 345 | 0)[i_baseAction][i_flagToProcess]["gpioPresence"]["pin"]; |
| 346 | |
| 347 | // get the pin value |
| 348 | uint8_t l_presencePinValue = |
| 349 | i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 350 | 0)[i_baseAction][i_flagToProcess]["gpioPresence"]["value"]; |
| 351 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 352 | gpiod::line l_presenceLine = gpiod::find_line(l_presencePinName); |
| 353 | |
| 354 | if (!l_presenceLine) |
| 355 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 356 | o_errCode = error_code::DEVICE_PRESENCE_UNKNOWN; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 357 | throw GpioException("Couldn't find the GPIO line."); |
| 358 | } |
| 359 | |
| 360 | l_presenceLine.request({"Read the presence line", |
| 361 | gpiod::line_request::DIRECTION_INPUT, 0}); |
| 362 | |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 363 | if (l_presencePinValue != l_presenceLine.get_value()) |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 364 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 365 | // As false is being returned in this case, caller needs to know |
| 366 | // that it is not due to some exception. It is because the pin was |
| 367 | // read correctly but was not having expected value. |
| 368 | o_errCode = error_code::DEVICE_NOT_PRESENT; |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 372 | return true; |
| 373 | } |
| 374 | catch (const std::exception& l_ex) |
| 375 | { |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 376 | std::string l_errMsg = "Exception on GPIO line: "; |
| 377 | l_errMsg += l_presencePinName; |
| 378 | l_errMsg += " Reason: "; |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 379 | l_errMsg += l_ex.what(); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 380 | l_errMsg += " File: " + i_vpdFilePath + " Pel Logged"; |
| 381 | |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 382 | uint16_t l_errCode = 0; |
| 383 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 384 | // ToDo -- Update Internal Rc code. |
| 385 | EventLogger::createAsyncPelWithInventoryCallout( |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 386 | EventLogger::getErrorType(l_ex), types::SeverityType::Informational, |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 387 | {{getInventoryObjPathFromJson(i_parsedConfigJson, i_vpdFilePath, |
| 388 | l_errCode), |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 389 | types::CalloutPriority::High}}, |
| 390 | std::source_location::current().file_name(), |
| 391 | std::source_location::current().function_name(), 0, l_errMsg, |
| 392 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
| 393 | |
| 394 | logging::logMessage(l_errMsg); |
| 395 | |
| 396 | // Except when GPIO pin value is false, we go and try collecting the |
| 397 | // FRU VPD as we couldn't able to read GPIO pin value due to some |
| 398 | // error/exception. So returning true in error scenario. |
| 399 | return true; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * @brief Process "setGpio" tag for a given FRU. |
| 405 | * |
| 406 | * This API enables the GPIO line. |
| 407 | * |
| 408 | * @param[in] i_parsedConfigJson - config JSON |
| 409 | * @param[in] i_vpdFilePath - EEPROM file path |
| 410 | * @param[in] i_baseAction - Base action for which this tag has been called. |
| 411 | * @param[in] i_flagToProcess - Flag nested under the base action for which this |
| 412 | * tag has been called. |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 413 | * @param[out] o_errCode - To set error code in case of error |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 414 | * @return Execution status. |
| 415 | */ |
| 416 | inline bool procesSetGpioTag( |
| 417 | const nlohmann::json& i_parsedConfigJson, const std::string& i_vpdFilePath, |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 418 | const std::string& i_baseAction, const std::string& i_flagToProcess, |
| 419 | uint16_t& o_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 420 | { |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 421 | std::string l_pinName; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 422 | try |
| 423 | { |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 424 | if (i_vpdFilePath.empty() || i_parsedConfigJson.empty() || |
| 425 | i_baseAction.empty() || i_flagToProcess.empty()) |
| 426 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 427 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 428 | return false; |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | if (!(((i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 432 | 0)[i_baseAction][i_flagToProcess]["setGpio"]) |
| 433 | .contains("pin")) && |
| 434 | ((i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 435 | 0)[i_baseAction][i_flagToProcess]["setGpio"]) |
| 436 | .contains("value")))) |
| 437 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 438 | o_errCode = error_code::JSON_MISSING_GPIO_INFO; |
| 439 | return false; |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | l_pinName = i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 443 | 0)[i_baseAction][i_flagToProcess]["setGpio"]["pin"]; |
| 444 | |
| 445 | // Get the value to set |
| 446 | uint8_t l_pinValue = i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 447 | 0)[i_baseAction][i_flagToProcess]["setGpio"]["value"]; |
| 448 | |
| 449 | logging::logMessage( |
| 450 | "Setting GPIO: " + l_pinName + " to " + std::to_string(l_pinValue)); |
| 451 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 452 | gpiod::line l_outputLine = gpiod::find_line(l_pinName); |
| 453 | |
| 454 | if (!l_outputLine) |
| 455 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 456 | o_errCode = error_code::GPIO_LINE_EXCEPTION; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 457 | throw GpioException("Couldn't find GPIO line."); |
| 458 | } |
| 459 | |
| 460 | l_outputLine.request( |
| 461 | {"FRU Action", ::gpiod::line_request::DIRECTION_OUTPUT, 0}, |
| 462 | l_pinValue); |
| 463 | return true; |
| 464 | } |
Sunny Srivastava | 0a5fce1 | 2025-04-09 11:09:51 +0530 | [diff] [blame] | 465 | catch (const std::exception& l_ex) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 466 | { |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 467 | std::string l_errMsg = "Exception on GPIO line: "; |
| 468 | l_errMsg += l_pinName; |
| 469 | l_errMsg += " Reason: "; |
| 470 | l_errMsg += l_ex.what(); |
| 471 | l_errMsg += " File: " + i_vpdFilePath + " Pel Logged"; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 472 | |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 473 | uint16_t l_errCode = 0; |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 474 | |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 475 | // ToDo -- Update Internal RC code |
| 476 | EventLogger::createAsyncPelWithInventoryCallout( |
| 477 | EventLogger::getErrorType(l_ex), types::SeverityType::Informational, |
| 478 | {{getInventoryObjPathFromJson(i_parsedConfigJson, i_vpdFilePath, |
| 479 | l_errCode), |
| 480 | types::CalloutPriority::High}}, |
| 481 | std::source_location::current().file_name(), |
| 482 | std::source_location::current().function_name(), 0, l_errMsg, |
| 483 | std::nullopt, std::nullopt, std::nullopt, std::nullopt); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 484 | |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 485 | logging::logMessage(l_errMsg); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 486 | |
| 487 | return false; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * @brief Process any action, if defined in config JSON. |
| 493 | * |
| 494 | * If any FRU(s) requires any special handling, then this base action can be |
| 495 | * defined for that FRU in the config JSON, processing of which will be handled |
| 496 | * in this API. |
| 497 | * Examples of action - preAction, PostAction etc. |
| 498 | * |
| 499 | * @param[in] i_parsedConfigJson - config JSON |
| 500 | * @param[in] i_action - Base action to be performed. |
| 501 | * @param[in] i_vpdFilePath - EEPROM file path |
| 502 | * @param[in] i_flagToProcess - To identify which flag(s) needs to be processed |
| 503 | * under PreAction tag of config JSON. |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 504 | * @param[out] o_errCode - To set error code in case of error. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 505 | * @return - success or failure |
| 506 | */ |
| 507 | inline bool executeBaseAction( |
| 508 | const nlohmann::json& i_parsedConfigJson, const std::string& i_action, |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 509 | const std::string& i_vpdFilePath, const std::string& i_flagToProcess, |
| 510 | uint16_t& o_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 511 | { |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 512 | if (i_flagToProcess.empty() || i_action.empty() || i_vpdFilePath.empty() || |
| 513 | !i_parsedConfigJson.contains("frus")) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 514 | { |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 515 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 516 | return false; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 517 | } |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 518 | if (!i_parsedConfigJson["frus"].contains(i_vpdFilePath)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 519 | { |
Sunny Srivastava | 4f053df | 2025-09-03 02:27:37 -0500 | [diff] [blame] | 520 | o_errCode = error_code::FILE_NOT_FOUND; |
| 521 | return false; |
| 522 | } |
| 523 | if (!i_parsedConfigJson["frus"][i_vpdFilePath].at(0).contains(i_action)) |
| 524 | { |
| 525 | o_errCode = error_code::MISSING_ACTION_TAG; |
| 526 | return false; |
| 527 | } |
| 528 | |
| 529 | if (!(i_parsedConfigJson["frus"][i_vpdFilePath].at(0))[i_action].contains( |
| 530 | i_flagToProcess)) |
| 531 | { |
| 532 | o_errCode = error_code::MISSING_FLAG; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 533 | return false; |
| 534 | } |
| 535 | |
| 536 | const nlohmann::json& l_tagsJson = |
| 537 | (i_parsedConfigJson["frus"][i_vpdFilePath].at( |
| 538 | 0))[i_action][i_flagToProcess]; |
| 539 | |
| 540 | for (const auto& l_tag : l_tagsJson.items()) |
| 541 | { |
| 542 | auto itrToFunction = funcionMap.find(l_tag.key()); |
| 543 | if (itrToFunction != funcionMap.end()) |
| 544 | { |
| 545 | if (!itrToFunction->second(i_parsedConfigJson, i_vpdFilePath, |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 546 | i_action, i_flagToProcess, o_errCode)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 547 | { |
| 548 | // In case any of the tag fails to execute. Mark action |
| 549 | // as failed for that flag. |
Sunny Srivastava | 84c3d23 | 2025-09-03 00:47:10 -0500 | [diff] [blame] | 550 | if (o_errCode) |
| 551 | { |
| 552 | logging::logMessage( |
| 553 | l_tag.key() + " failed for [" + i_vpdFilePath + |
| 554 | "]. Reason " + |
| 555 | vpdSpecificUtility::getErrCodeMsg(o_errCode)); |
| 556 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | return true; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * @brief Get redundant FRU path from system config JSON |
| 567 | * |
| 568 | * Given either D-bus inventory path/FRU path/redundant FRU path, this |
| 569 | * API returns the redundant FRU path taken from "redundantEeprom" tag from |
| 570 | * system config JSON. |
| 571 | * |
| 572 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 573 | * @param[in] i_vpdPath - Path to where VPD is stored. |
| 574 | * |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 575 | * @return On success return valid path, on failure return empty string. |
| 576 | */ |
| 577 | inline std::string getRedundantEepromPathFromJson( |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 578 | const nlohmann::json& i_sysCfgJsonObj, |
| 579 | const std::string& i_vpdPath) noexcept |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 580 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 581 | try |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 582 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 583 | if (i_vpdPath.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 584 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 585 | throw std::runtime_error("Path parameter is empty."); |
| 586 | } |
| 587 | |
| 588 | if (!i_sysCfgJsonObj.contains("frus")) |
| 589 | { |
| 590 | throw std::runtime_error("Missing frus tag in system config JSON."); |
| 591 | } |
| 592 | |
| 593 | // check if given path is FRU path |
| 594 | if (i_sysCfgJsonObj["frus"].contains(i_vpdPath)) |
| 595 | { |
| 596 | return i_sysCfgJsonObj["frus"][i_vpdPath].at(0).value( |
| 597 | "redundantEeprom", ""); |
| 598 | } |
| 599 | |
| 600 | const nlohmann::json& l_fruList = |
| 601 | i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 602 | |
| 603 | for (const auto& l_fru : l_fruList.items()) |
| 604 | { |
| 605 | const std::string& l_fruPath = l_fru.key(); |
| 606 | const std::string& l_redundantFruPath = |
| 607 | i_sysCfgJsonObj["frus"][l_fruPath].at(0).value( |
| 608 | "redundantEeprom", ""); |
| 609 | |
| 610 | // check if given path is inventory path or redundant FRU path |
| 611 | if ((i_sysCfgJsonObj["frus"][l_fruPath].at(0).value( |
| 612 | "inventoryPath", "") == i_vpdPath) || |
| 613 | (l_redundantFruPath == i_vpdPath)) |
| 614 | { |
| 615 | return l_redundantFruPath; |
| 616 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 617 | } |
| 618 | } |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 619 | catch (const std::exception& l_ex) |
| 620 | { |
| 621 | logging::logMessage("Failed to get redundant EEPROM path, error: " + |
| 622 | std::string(l_ex.what())); |
| 623 | } |
| 624 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 625 | return std::string(); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * @brief Get FRU EEPROM path from system config JSON |
| 630 | * |
| 631 | * Given either D-bus inventory path/FRU EEPROM path/redundant EEPROM path, |
| 632 | * this API returns FRU EEPROM path if present in JSON. |
| 633 | * |
| 634 | * @param[in] i_sysCfgJsonObj - System config JSON object |
| 635 | * @param[in] i_vpdPath - Path to where VPD is stored. |
| 636 | * |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 637 | * @return On success return valid path, on failure return empty string. |
| 638 | */ |
| 639 | inline std::string getFruPathFromJson(const nlohmann::json& i_sysCfgJsonObj, |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 640 | const std::string& i_vpdPath) noexcept |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 641 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 642 | try |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 643 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 644 | if (i_vpdPath.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 645 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 646 | throw std::runtime_error("Path parameter is empty."); |
| 647 | } |
| 648 | |
| 649 | if (!i_sysCfgJsonObj.contains("frus")) |
| 650 | { |
| 651 | throw std::runtime_error("Missing frus tag in system config JSON."); |
| 652 | } |
| 653 | |
| 654 | // check if given path is FRU path |
| 655 | if (i_sysCfgJsonObj["frus"].contains(i_vpdPath)) |
| 656 | { |
| 657 | return i_vpdPath; |
| 658 | } |
| 659 | |
| 660 | const nlohmann::json& l_fruList = |
| 661 | i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 662 | |
| 663 | for (const auto& l_fru : l_fruList.items()) |
| 664 | { |
| 665 | const auto l_fruPath = l_fru.key(); |
| 666 | |
| 667 | // check if given path is redundant FRU path or inventory path |
| 668 | if (i_vpdPath == i_sysCfgJsonObj["frus"][l_fruPath].at(0).value( |
| 669 | "redundantEeprom", "") || |
| 670 | (i_vpdPath == i_sysCfgJsonObj["frus"][l_fruPath].at(0).value( |
| 671 | "inventoryPath", ""))) |
| 672 | { |
| 673 | return l_fruPath; |
| 674 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 675 | } |
| 676 | } |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 677 | catch (const std::exception& l_ex) |
| 678 | { |
| 679 | logging::logMessage("Failed to get FRU path from JSON, error: " + |
| 680 | std::string(l_ex.what())); |
| 681 | } |
| 682 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 683 | return std::string(); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * @brief An API to check backup and restore VPD is required. |
| 688 | * |
| 689 | * The API checks if there is provision for backup and restore mentioned in the |
| 690 | * system config JSON, by looking "backupRestoreConfigPath" tag. |
| 691 | * Checks if the path mentioned is a hardware path, by checking if the file path |
| 692 | * exists and size of contents in the path. |
| 693 | * |
| 694 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 695 | * |
| 696 | * @return true if backup and restore is required, false otherwise. |
| 697 | */ |
| 698 | inline bool isBackupAndRestoreRequired(const nlohmann::json& i_sysCfgJsonObj) |
| 699 | { |
| 700 | try |
| 701 | { |
| 702 | const std::string& l_backupAndRestoreCfgFilePath = |
| 703 | i_sysCfgJsonObj.value("backupRestoreConfigPath", ""); |
| 704 | if (!l_backupAndRestoreCfgFilePath.empty() && |
| 705 | std::filesystem::exists(l_backupAndRestoreCfgFilePath) && |
| 706 | !std::filesystem::is_empty(l_backupAndRestoreCfgFilePath)) |
| 707 | { |
| 708 | return true; |
| 709 | } |
| 710 | } |
| 711 | catch (std::exception& ex) |
| 712 | { |
| 713 | logging::logMessage(ex.what()); |
| 714 | } |
| 715 | return false; |
| 716 | } |
| 717 | |
| 718 | /** @brief API to check if an action is required for given EEPROM path. |
| 719 | * |
| 720 | * System config JSON can contain pre-action, post-action etc. like actions |
| 721 | * defined for an EEPROM path. The API will check if any such action is defined |
| 722 | * for the EEPROM. |
| 723 | * |
| 724 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 725 | * @param[in] i_vpdFruPath - EEPROM path. |
| 726 | * @param[in] i_action - Action to be checked. |
| 727 | * @param[in] i_flowFlag - Denotes the flow w.r.t which the action should be |
| 728 | * triggered. |
| 729 | * @return - True if action is defined for the flow, false otherwise. |
| 730 | */ |
| 731 | inline bool isActionRequired( |
| 732 | const nlohmann::json& i_sysCfgJsonObj, const std::string& i_vpdFruPath, |
| 733 | const std::string& i_action, const std::string& i_flowFlag) |
| 734 | { |
| 735 | if (i_vpdFruPath.empty() || i_action.empty() || i_flowFlag.empty()) |
| 736 | { |
| 737 | logging::logMessage("Invalid parameters recieved."); |
| 738 | return false; |
| 739 | } |
| 740 | |
| 741 | if (!i_sysCfgJsonObj.contains("frus")) |
| 742 | { |
| 743 | logging::logMessage("Invalid JSON object recieved."); |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | if (!i_sysCfgJsonObj["frus"].contains(i_vpdFruPath)) |
| 748 | { |
| 749 | logging::logMessage( |
| 750 | "JSON object does not contain EEPROM path " + i_vpdFruPath); |
| 751 | return false; |
| 752 | } |
| 753 | |
| 754 | if ((i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0)).contains(i_action)) |
| 755 | { |
| 756 | if ((i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0))[i_action].contains( |
| 757 | i_flowFlag)) |
| 758 | { |
| 759 | return true; |
| 760 | } |
| 761 | |
| 762 | logging::logMessage("Flow flag: [" + i_flowFlag + |
Souvik Roy | 410d96c | 2025-05-09 02:30:15 -0500 | [diff] [blame] | 763 | "], not found in JSON for path: " + i_vpdFruPath + |
| 764 | " while checking for action: " + i_action); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 765 | return false; |
| 766 | } |
| 767 | return false; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * @brief An API to return list of FRUs that needs GPIO polling. |
| 772 | * |
| 773 | * An API that checks for the FRUs that requires GPIO polling and returns |
| 774 | * a list of FRUs that needs polling. Returns an empty list if there are |
| 775 | * no FRUs that requires polling. |
| 776 | * |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 777 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 778 | * |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 779 | * @return On success list of FRUs parameters that needs polling. On failure, |
| 780 | * empty list. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 781 | */ |
Patrick Williams | 43fedab | 2025-02-03 14:28:05 -0500 | [diff] [blame] | 782 | inline std::vector<std::string> getListOfGpioPollingFrus( |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 783 | const nlohmann::json& i_sysCfgJsonObj) noexcept |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 784 | { |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 785 | std::vector<std::string> l_gpioPollingRequiredFrusList; |
| 786 | |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 787 | try |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 788 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 789 | if (i_sysCfgJsonObj.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 790 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 791 | throw std::runtime_error("Invalid Parameters"); |
| 792 | } |
| 793 | |
| 794 | if (!i_sysCfgJsonObj.contains("frus")) |
| 795 | { |
| 796 | throw std::runtime_error( |
| 797 | "Missing frus section in system config JSON"); |
| 798 | } |
| 799 | |
| 800 | for (const auto& l_fru : i_sysCfgJsonObj["frus"].items()) |
| 801 | { |
| 802 | const auto l_fruPath = l_fru.key(); |
| 803 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 804 | if (isActionRequired(i_sysCfgJsonObj, l_fruPath, "pollingRequired", |
| 805 | "hotPlugging")) |
| 806 | { |
| 807 | if (i_sysCfgJsonObj["frus"][l_fruPath] |
| 808 | .at(0)["pollingRequired"]["hotPlugging"] |
| 809 | .contains("gpioPresence")) |
| 810 | { |
| 811 | l_gpioPollingRequiredFrusList.push_back(l_fruPath); |
| 812 | } |
| 813 | } |
| 814 | } |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 815 | } |
| 816 | catch (const std::exception& l_ex) |
| 817 | { |
| 818 | logging::logMessage("Failed to get list of GPIO polling FRUs, error: " + |
| 819 | std::string(l_ex.what())); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | return l_gpioPollingRequiredFrusList; |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * @brief Get all related path(s) to update keyword value. |
| 827 | * |
| 828 | * Given FRU EEPROM path/Inventory path needs keyword's value update, this API |
| 829 | * returns tuple of FRU EEPROM path, inventory path and redundant EEPROM path if |
| 830 | * exists in the system config JSON. |
| 831 | * |
| 832 | * Note: If the inventory object path or redundant EEPROM path(s) are not found |
| 833 | * in the system config JSON, corresponding fields will have empty value in the |
| 834 | * returning tuple. |
| 835 | * |
| 836 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 837 | * @param[in,out] io_vpdPath - Inventory object path or FRU EEPROM path. |
| 838 | * |
| 839 | * @return On success returns tuple of EEPROM path, inventory path & redundant |
| 840 | * path, on failure returns tuple with given input path alone. |
| 841 | */ |
| 842 | inline std::tuple<std::string, std::string, std::string> |
| 843 | getAllPathsToUpdateKeyword(const nlohmann::json& i_sysCfgJsonObj, |
| 844 | std::string io_vpdPath) |
| 845 | { |
| 846 | types::Path l_inventoryObjPath; |
| 847 | types::Path l_redundantFruPath; |
| 848 | try |
| 849 | { |
| 850 | if (!i_sysCfgJsonObj.empty()) |
| 851 | { |
| 852 | // Get hardware path from system config JSON. |
| 853 | const types::Path l_fruPath = |
| 854 | jsonUtility::getFruPathFromJson(i_sysCfgJsonObj, io_vpdPath); |
| 855 | |
| 856 | if (!l_fruPath.empty()) |
| 857 | { |
| 858 | io_vpdPath = l_fruPath; |
| 859 | |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 860 | uint16_t l_errCode = 0; |
| 861 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 862 | // Get inventory object path from system config JSON |
| 863 | l_inventoryObjPath = jsonUtility::getInventoryObjPathFromJson( |
Rekha Aparna | 017567a | 2025-08-13 02:07:06 -0500 | [diff] [blame] | 864 | i_sysCfgJsonObj, l_fruPath, l_errCode); |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 865 | |
| 866 | // Get redundant hardware path if present in system config JSON |
| 867 | l_redundantFruPath = |
| 868 | jsonUtility::getRedundantEepromPathFromJson(i_sysCfgJsonObj, |
| 869 | l_fruPath); |
| 870 | } |
| 871 | } |
| 872 | } |
| 873 | catch (const std::exception& l_exception) |
| 874 | { |
| 875 | logging::logMessage( |
| 876 | "Failed to get all paths to update keyword value, error " + |
| 877 | std::string(l_exception.what())); |
| 878 | } |
| 879 | return std::make_tuple(io_vpdPath, l_inventoryObjPath, l_redundantFruPath); |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * @brief An API to get DBus service name. |
| 884 | * |
| 885 | * Given DBus inventory path, this API returns DBus service name if present in |
| 886 | * the JSON. |
| 887 | * |
| 888 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 889 | * @param[in] l_inventoryPath - DBus inventory path. |
| 890 | * |
| 891 | * @return On success returns the service name present in the system config |
| 892 | * JSON, otherwise empty string. |
| 893 | * |
| 894 | * Note: Caller has to handle in case of empty string received. |
| 895 | */ |
| 896 | inline std::string getServiceName(const nlohmann::json& i_sysCfgJsonObj, |
| 897 | const std::string& l_inventoryPath) |
| 898 | { |
| 899 | try |
| 900 | { |
| 901 | if (l_inventoryPath.empty()) |
| 902 | { |
| 903 | throw std::runtime_error("Path parameter is empty."); |
| 904 | } |
| 905 | |
| 906 | if (!i_sysCfgJsonObj.contains("frus")) |
| 907 | { |
| 908 | throw std::runtime_error("Missing frus tag in system config JSON."); |
| 909 | } |
| 910 | |
| 911 | const nlohmann::json& l_listOfFrus = |
| 912 | i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 913 | |
| 914 | for (const auto& l_frus : l_listOfFrus.items()) |
| 915 | { |
| 916 | for (const auto& l_inventoryItem : l_frus.value()) |
| 917 | { |
| 918 | if (l_inventoryPath.compare(l_inventoryItem["inventoryPath"]) == |
| 919 | constants::STR_CMP_SUCCESS) |
| 920 | { |
| 921 | return l_inventoryItem["serviceName"]; |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | throw std::runtime_error( |
| 926 | "Inventory path not found in the system config JSON"); |
| 927 | } |
| 928 | catch (const std::exception& l_exception) |
| 929 | { |
| 930 | logging::logMessage( |
| 931 | "Error while getting DBus service name for given path " + |
| 932 | l_inventoryPath + ", error: " + std::string(l_exception.what())); |
| 933 | // TODO:log PEL |
| 934 | } |
| 935 | return std::string{}; |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * @brief An API to check if a FRU is tagged as "powerOffOnly" |
| 940 | * |
| 941 | * Given the system config JSON and VPD FRU path, this API checks if the FRU |
| 942 | * VPD can be collected at Chassis Power Off state only. |
| 943 | * |
| 944 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 945 | * @param[in] i_vpdFruPath - EEPROM path. |
| 946 | * @return - True if FRU VPD can be collected at Chassis Power Off state only. |
| 947 | * False otherwise |
| 948 | */ |
| 949 | inline bool isFruPowerOffOnly(const nlohmann::json& i_sysCfgJsonObj, |
| 950 | const std::string& i_vpdFruPath) |
| 951 | { |
| 952 | if (i_vpdFruPath.empty()) |
| 953 | { |
| 954 | logging::logMessage("FRU path is empty."); |
| 955 | return false; |
| 956 | } |
| 957 | |
| 958 | if (!i_sysCfgJsonObj.contains("frus")) |
| 959 | { |
| 960 | logging::logMessage("Missing frus tag in system config JSON."); |
| 961 | return false; |
| 962 | } |
| 963 | |
| 964 | if (!i_sysCfgJsonObj["frus"].contains(i_vpdFruPath)) |
| 965 | { |
| 966 | logging::logMessage("JSON object does not contain EEPROM path \'" + |
| 967 | i_vpdFruPath + "\'"); |
| 968 | return false; |
| 969 | } |
| 970 | |
| 971 | return ((i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0)) |
| 972 | .contains("powerOffOnly") && |
| 973 | (i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0)["powerOffOnly"])); |
| 974 | } |
| 975 | |
| 976 | /** |
| 977 | * @brief API which tells if the FRU is replaceable at runtime |
| 978 | * |
| 979 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 980 | * @param[in] i_vpdFruPath - EEPROM path. |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 981 | * @param[out] o_errCode - to set error code in case of error. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 982 | * |
| 983 | * @return true if FRU is replaceable at runtime. false otherwise. |
| 984 | */ |
| 985 | inline bool isFruReplaceableAtRuntime(const nlohmann::json& i_sysCfgJsonObj, |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 986 | const std::string& i_vpdFruPath, |
| 987 | uint16_t& o_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 988 | { |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 989 | if (i_vpdFruPath.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 990 | { |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 991 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 992 | return false; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 993 | } |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 994 | |
| 995 | if (i_sysCfgJsonObj.empty() || (!i_sysCfgJsonObj.contains("frus"))) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 996 | { |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 997 | o_errCode = error_code::INVALID_JSON; |
| 998 | return false; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 999 | } |
| 1000 | |
Rekha Aparna | ad0db9e | 2025-09-01 20:29:18 -0500 | [diff] [blame] | 1001 | return ( |
| 1002 | (i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0)) |
| 1003 | .contains("replaceableAtRuntime") && |
| 1004 | (i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0)["replaceableAtRuntime"])); |
| 1005 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1006 | return false; |
| 1007 | } |
| 1008 | |
| 1009 | /** |
| 1010 | * @brief API which tells if the FRU is replaceable at standby |
| 1011 | * |
| 1012 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 1013 | * @param[in] i_vpdFruPath - EEPROM path. |
Rekha Aparna | 4084561 | 2025-09-01 19:58:56 -0500 | [diff] [blame] | 1014 | * @param[out] o_errCode - set error code in case of error. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1015 | * |
| 1016 | * @return true if FRU is replaceable at standby. false otherwise. |
| 1017 | */ |
| 1018 | inline bool isFruReplaceableAtStandby(const nlohmann::json& i_sysCfgJsonObj, |
Rekha Aparna | 4084561 | 2025-09-01 19:58:56 -0500 | [diff] [blame] | 1019 | const std::string& i_vpdFruPath, |
| 1020 | uint16_t& o_errCode) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1021 | { |
Rekha Aparna | 4084561 | 2025-09-01 19:58:56 -0500 | [diff] [blame] | 1022 | if (i_vpdFruPath.empty()) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1023 | { |
Rekha Aparna | 4084561 | 2025-09-01 19:58:56 -0500 | [diff] [blame] | 1024 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 1025 | return false; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1026 | } |
Rekha Aparna | 4084561 | 2025-09-01 19:58:56 -0500 | [diff] [blame] | 1027 | |
| 1028 | if (i_sysCfgJsonObj.empty() || (!i_sysCfgJsonObj.contains("frus"))) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1029 | { |
Rekha Aparna | 4084561 | 2025-09-01 19:58:56 -0500 | [diff] [blame] | 1030 | o_errCode = error_code::INVALID_JSON; |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1031 | } |
| 1032 | |
Rekha Aparna | 4084561 | 2025-09-01 19:58:56 -0500 | [diff] [blame] | 1033 | return ( |
| 1034 | (i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0)) |
| 1035 | .contains("replaceableAtStandby") && |
| 1036 | (i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0)["replaceableAtStandby"])); |
| 1037 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1038 | return false; |
| 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * @brief API to get list of FRUs replaceable at standby from JSON. |
| 1043 | * |
| 1044 | * The API will return a vector of FRUs inventory path which are replaceable at |
| 1045 | * standby. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1046 | * |
| 1047 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 1048 | * |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 1049 | * @return - On success, list of FRUs replaceable at standby. On failure, empty |
| 1050 | * vector. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1051 | */ |
Patrick Williams | 43fedab | 2025-02-03 14:28:05 -0500 | [diff] [blame] | 1052 | inline std::vector<std::string> getListOfFrusReplaceableAtStandby( |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 1053 | const nlohmann::json& i_sysCfgJsonObj) noexcept |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1054 | { |
| 1055 | std::vector<std::string> l_frusReplaceableAtStandby; |
| 1056 | |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 1057 | try |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1058 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 1059 | if (!i_sysCfgJsonObj.contains("frus")) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1060 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 1061 | throw std::runtime_error("Missing frus tag in system config JSON."); |
| 1062 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1063 | |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 1064 | const nlohmann::json& l_fruList = |
| 1065 | i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>(); |
| 1066 | |
| 1067 | for (const auto& l_fru : l_fruList.items()) |
| 1068 | { |
| 1069 | if (i_sysCfgJsonObj["frus"][l_fru.key()].at(0).value( |
| 1070 | "replaceableAtStandby", false)) |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1071 | { |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 1072 | const std::string& l_inventoryObjectPath = |
| 1073 | i_sysCfgJsonObj["frus"][l_fru.key()].at(0).value( |
| 1074 | "inventoryPath", ""); |
| 1075 | |
| 1076 | if (!l_inventoryObjectPath.empty()) |
| 1077 | { |
| 1078 | l_frusReplaceableAtStandby.emplace_back( |
| 1079 | l_inventoryObjectPath); |
| 1080 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | } |
RekhaAparna01 | 7fea9f5 | 2025-02-17 04:14:02 -0600 | [diff] [blame] | 1084 | catch (const std::exception& l_ex) |
| 1085 | { |
| 1086 | logging::logMessage( |
| 1087 | "Failed to get list of FRUs replaceable at standby, error: " + |
| 1088 | std::string(l_ex.what())); |
| 1089 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1090 | |
| 1091 | return l_frusReplaceableAtStandby; |
| 1092 | } |
| 1093 | |
Sunny Srivastava | 022112b | 2025-02-19 19:53:29 +0530 | [diff] [blame] | 1094 | /** |
| 1095 | * @brief API to select powerVS JSON based on system IM. |
| 1096 | * |
| 1097 | * The API selects respective JSON based on system IM, parse it and return the |
| 1098 | * JSON object. Empty JSON will be returned in case of any error. Caller needs |
| 1099 | * to handle empty value. |
| 1100 | * |
| 1101 | * @param[in] i_imValue - IM value of the system. |
| 1102 | * @return Parsed JSON object, empty JSON otherwise. |
| 1103 | */ |
| 1104 | inline nlohmann::json getPowerVsJson(const types::BinaryVector& i_imValue) |
| 1105 | { |
| 1106 | try |
| 1107 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 1108 | uint16_t l_errCode = 0; |
Sunny Srivastava | 022112b | 2025-02-19 19:53:29 +0530 | [diff] [blame] | 1109 | if ((i_imValue.at(0) == constants::HEX_VALUE_50) && |
| 1110 | (i_imValue.at(1) == constants::HEX_VALUE_00) && |
| 1111 | (i_imValue.at(2) == constants::HEX_VALUE_30)) |
| 1112 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 1113 | nlohmann::json l_parsedJson = jsonUtility::getParsedJson( |
| 1114 | constants::power_vs_50003_json, l_errCode); |
| 1115 | |
| 1116 | if (l_errCode) |
| 1117 | { |
| 1118 | logging::logMessage( |
| 1119 | "Failed to parse JSON file [ " + |
| 1120 | std::string(constants::power_vs_50003_json) + |
| 1121 | " ], error : " + |
| 1122 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1123 | } |
| 1124 | |
| 1125 | return l_parsedJson; |
Sunny Srivastava | 022112b | 2025-02-19 19:53:29 +0530 | [diff] [blame] | 1126 | } |
| 1127 | else if (i_imValue.at(0) == constants::HEX_VALUE_50 && |
| 1128 | (i_imValue.at(1) == constants::HEX_VALUE_00) && |
| 1129 | (i_imValue.at(2) == constants::HEX_VALUE_10)) |
| 1130 | { |
Rekha Aparna | ca9a086 | 2025-08-29 04:08:33 -0500 | [diff] [blame] | 1131 | nlohmann::json l_parsedJson = jsonUtility::getParsedJson( |
| 1132 | constants::power_vs_50001_json, l_errCode); |
| 1133 | |
| 1134 | if (l_errCode) |
| 1135 | { |
| 1136 | logging::logMessage( |
| 1137 | "Failed to parse JSON file [ " + |
| 1138 | std::string(constants::power_vs_50001_json) + |
| 1139 | " ], error : " + |
| 1140 | vpdSpecificUtility::getErrCodeMsg(l_errCode)); |
| 1141 | } |
| 1142 | |
| 1143 | return l_parsedJson; |
Sunny Srivastava | 022112b | 2025-02-19 19:53:29 +0530 | [diff] [blame] | 1144 | } |
| 1145 | return nlohmann::json{}; |
| 1146 | } |
| 1147 | catch (const std::exception& l_ex) |
| 1148 | { |
| 1149 | return nlohmann::json{}; |
| 1150 | } |
| 1151 | } |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1152 | |
| 1153 | /** |
| 1154 | * @brief API to get list of FRUs for which "monitorPresence" is true. |
| 1155 | * |
| 1156 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
Rekha Aparna | 2362bed | 2025-09-01 13:18:40 -0500 | [diff] [blame] | 1157 | * @param[out] o_errCode - To set error code in case of error. |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1158 | * |
| 1159 | * @return On success, returns list of FRUs for which "monitorPresence" is true, |
| 1160 | * empty list on error. |
| 1161 | */ |
| 1162 | inline std::vector<types::Path> getFrusWithPresenceMonitoring( |
Rekha Aparna | 2362bed | 2025-09-01 13:18:40 -0500 | [diff] [blame] | 1163 | const nlohmann::json& i_sysCfgJsonObj, uint16_t& o_errCode) |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1164 | { |
| 1165 | std::vector<types::Path> l_frusWithPresenceMonitoring; |
Rekha Aparna | 2362bed | 2025-09-01 13:18:40 -0500 | [diff] [blame] | 1166 | |
| 1167 | if (!i_sysCfgJsonObj.contains("frus")) |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1168 | { |
Rekha Aparna | 2362bed | 2025-09-01 13:18:40 -0500 | [diff] [blame] | 1169 | o_errCode = error_code::INVALID_JSON; |
| 1170 | return l_frusWithPresenceMonitoring; |
| 1171 | } |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1172 | |
Rekha Aparna | 2362bed | 2025-09-01 13:18:40 -0500 | [diff] [blame] | 1173 | const nlohmann::json& l_listOfFrus = |
| 1174 | i_sysCfgJsonObj["frus"].get_ref<const nlohmann::json::object_t&>(); |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1175 | |
Rekha Aparna | 2362bed | 2025-09-01 13:18:40 -0500 | [diff] [blame] | 1176 | for (const auto& l_aFru : l_listOfFrus) |
| 1177 | { |
| 1178 | if (l_aFru.at(0).value("monitorPresence", false)) |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1179 | { |
Rekha Aparna | 2362bed | 2025-09-01 13:18:40 -0500 | [diff] [blame] | 1180 | l_frusWithPresenceMonitoring.emplace_back( |
| 1181 | l_aFru.at(0).value("inventoryPath", "")); |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1182 | } |
| 1183 | } |
Rekha Aparna | 2362bed | 2025-09-01 13:18:40 -0500 | [diff] [blame] | 1184 | |
Souvik Roy | 495eedd | 2025-07-02 02:13:43 -0500 | [diff] [blame] | 1185 | return l_frusWithPresenceMonitoring; |
| 1186 | } |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1187 | |
| 1188 | /** |
| 1189 | * @brief API which tells if the FRU's presence is handled |
| 1190 | * |
| 1191 | * For a given FRU, this API checks if it's presence is handled by vpd-manager |
| 1192 | * by checking the "handlePresence" tag. |
| 1193 | * |
| 1194 | * @param[in] i_sysCfgJsonObj - System config JSON object. |
| 1195 | * @param[in] i_vpdFruPath - EEPROM path. |
Rekha Aparna | a1187a5 | 2025-09-01 12:42:19 -0500 | [diff] [blame] | 1196 | * @param[out] o_errCode - To set error code in case of failure. |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1197 | * |
| 1198 | * @return true if FRU presence is handled, false otherwise. |
| 1199 | */ |
| 1200 | inline bool isFruPresenceHandled(const nlohmann::json& i_sysCfgJsonObj, |
Rekha Aparna | a1187a5 | 2025-09-01 12:42:19 -0500 | [diff] [blame] | 1201 | const std::string& i_vpdFruPath, |
| 1202 | uint16_t& o_errCode) |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1203 | { |
Rekha Aparna | a1187a5 | 2025-09-01 12:42:19 -0500 | [diff] [blame] | 1204 | if (i_vpdFruPath.empty()) |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1205 | { |
Rekha Aparna | a1187a5 | 2025-09-01 12:42:19 -0500 | [diff] [blame] | 1206 | o_errCode = error_code::INVALID_INPUT_PARAMETER; |
| 1207 | return false; |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1208 | } |
| 1209 | |
Rekha Aparna | a1187a5 | 2025-09-01 12:42:19 -0500 | [diff] [blame] | 1210 | if (!i_sysCfgJsonObj.contains("frus")) |
| 1211 | { |
| 1212 | o_errCode = error_code::INVALID_JSON; |
| 1213 | return false; |
| 1214 | } |
| 1215 | |
| 1216 | if (!i_sysCfgJsonObj["frus"].contains(i_vpdFruPath)) |
| 1217 | { |
| 1218 | o_errCode = error_code::FRU_PATH_NOT_FOUND; |
| 1219 | return false; |
| 1220 | } |
| 1221 | |
| 1222 | return i_sysCfgJsonObj["frus"][i_vpdFruPath].at(0).value( |
| 1223 | "handlePresence", true); |
Souvik Roy | e912015 | 2025-07-02 08:24:38 -0500 | [diff] [blame] | 1224 | } |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1225 | } // namespace jsonUtility |
| 1226 | } // namespace vpd |