1 #pragma once 2 3 #include "logger.hpp" 4 #include "parser_factory.hpp" 5 #include "parser_interface.hpp" 6 #include "types.hpp" 7 8 #include <string.h> 9 10 #include <nlohmann/json.hpp> 11 12 #include <iostream> 13 14 namespace vpd 15 { 16 /** 17 * @brief Class to implement a wrapper around concrete parser class. 18 * The class based on VPD file passed, selects the required parser and exposes 19 * API to parse the VPD and return the parsed data in required format to the 20 * caller. 21 */ 22 class Parser 23 { 24 public: 25 /** 26 * @brief Constructor 27 * 28 * @param[in] i_vpdFilePath - Path to the VPD file. 29 * @param[in] i_parsedJson - Parsed JSON. 30 * @param[in] i_vpdCollectionMode - VPD collection mode, default is hardware 31 * mode. 32 * 33 * @throw std::runtime_error 34 */ 35 Parser(const std::string& i_vpdFilePath, nlohmann::json i_parsedJson, 36 types::VpdCollectionMode i_vpdCollectionMode = 37 types::VpdCollectionMode::DEFAULT_MODE); 38 39 /** 40 * @brief API to implement a generic parsing logic. 41 * 42 * This API is called to select parser based on the vpd data extracted from 43 * the VPD file path passed to the constructor of the class. 44 * It further parses the data based on the parser selected and returned 45 * parsed map to the caller. 46 */ 47 types::VPDMapVariant parse(); 48 49 /** 50 * @brief API to get parser instance based on VPD type. 51 * 52 * This API detects the VPD type based on the file path passed to the 53 * constructor of the class and returns the respective parser instance. 54 * 55 * @return Parser instance. 56 */ 57 std::shared_ptr<vpd::ParserInterface> getVpdParserInstance(); 58 59 /** 60 * @brief Update keyword value. 61 * 62 * This API is used to update keyword value on the EEPROM path and its 63 * redundant path(s) if any taken from system config JSON. And also updates 64 * keyword value on DBus. 65 * 66 * To update IPZ type VPD, input parameter for writing should be in the form 67 * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}). 68 * 69 * To update Keyword type VPD, input parameter for writing should be in the 70 * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}). 71 * 72 * @param[in] i_paramsToWriteData - Input details. 73 * 74 * @return On success returns number of bytes written, on failure returns 75 * -1. 76 */ 77 int updateVpdKeyword(const types::WriteVpdParams& i_paramsToWriteData); 78 79 /** 80 * @brief Update keyword value. 81 * 82 * This API is used to update keyword value on the EEPROM path and its 83 * redundant path(s) if any taken from system config JSON. And also updates 84 * keyword value on DBus. 85 * 86 * To update IPZ type VPD, input parameter for writing should be in the form 87 * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}). 88 * 89 * To update Keyword type VPD, input parameter for writing should be in the 90 * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}). 91 * 92 * @param[in] i_paramsToWriteData - Input details. 93 * @param[in] o_updatedValue - Actual value which has been updated on 94 * hardware. 95 * 96 * @return On success returns number of bytes written, on failure returns 97 * -1. 98 */ 99 int updateVpdKeyword(const types::WriteVpdParams& i_paramsToWriteData, 100 types::DbusVariantType& o_updatedValue); 101 102 /** 103 * @brief Update keyword value on hardware. 104 * 105 * This API is used to update keyword value on the hardware path. 106 * 107 * To update IPZ type VPD, input parameter for writing should be in the form 108 * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}). 109 * 110 * To update Keyword type VPD, input parameter for writing should be in the 111 * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}). 112 * 113 * @param[in] i_paramsToWriteData - Input details. 114 * 115 * @return On success returns number of bytes written, on failure returns 116 * -1. 117 */ 118 int updateVpdKeywordOnHardware( 119 const types::WriteVpdParams& i_paramsToWriteData); 120 121 private: 122 /** 123 * @brief Update keyword value on redundant path. 124 * 125 * This API is used to update keyword value on the given redundant path(s). 126 * 127 * To update IPZ type VPD, input parameter for writing should be in the form 128 * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}). 129 * 130 * To update Keyword type VPD, input parameter for writing should be in the 131 * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}). 132 * 133 * @param[in] i_fruPath - Redundant EEPROM path. 134 * @param[in] i_paramsToWriteData - Input details. 135 * 136 * @return On success returns number of bytes written, on failure returns 137 * -1. 138 */ 139 int updateVpdKeywordOnRedundantPath( 140 const std::string& i_fruPath, 141 const types::WriteVpdParams& i_paramsToWriteData); 142 143 // holds offfset to VPD if applicable. 144 size_t m_vpdStartOffset = 0; 145 146 // Base VPD file path used for JSON lookups. 147 const std::string& m_vpdFilePath; 148 149 // Path to configuration file, can be empty. 150 nlohmann::json m_parsedJson; 151 152 // Vector to hold VPD. 153 types::BinaryVector m_vpdVector; 154 155 // VPD collection mode, default is hardware mode. 156 types::VpdCollectionMode m_vpdCollectionMode; 157 158 /** 159 * For mode-based VPD collection, m_vpdModeBasedFruPath is derived from base 160 * path m_vpdFilePath and represents the actual file location. 161 */ 162 std::string m_vpdModeBasedFruPath; 163 164 // shared pointer to Logger object. 165 std::shared_ptr<Logger> m_logger; 166 }; // parser 167 } // namespace vpd 168