blob: 9af308899a5cd2cc5e4545198219bacc39a1e45f [file] [log] [blame]
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001#pragma once
2
3#include "parser_factory.hpp"
4#include "parser_interface.hpp"
5#include "types.hpp"
6
7#include <string.h>
8
9#include <nlohmann/json.hpp>
10
11#include <iostream>
12
13namespace vpd
14{
15/**
16 * @brief Class to implement a wrapper around concrete parser class.
17 * The class based on VPD file passed, selects the required parser and exposes
18 * API to parse the VPD and return the parsed data in required format to the
19 * caller.
20 */
21class Parser
22{
23 public:
24 /**
25 * @brief Constructor
26 *
27 * @param[in] vpdFilePath - Path to the VPD file.
28 * @param[in] parsedJson - Parsed JSON.
29 */
30 Parser(const std::string& vpdFilePath, nlohmann::json parsedJson);
31
32 /**
33 * @brief API to implement a generic parsing logic.
34 *
35 * This API is called to select parser based on the vpd data extracted from
36 * the VPD file path passed to the constructor of the class.
37 * It further parses the data based on the parser selected and returned
38 * parsed map to the caller.
39 */
40 types::VPDMapVariant parse();
41
42 /**
43 * @brief API to get parser instance based on VPD type.
44 *
45 * This API detects the VPD type based on the file path passed to the
46 * constructor of the class and returns the respective parser instance.
47 *
48 * @return Parser instance.
49 */
50 std::shared_ptr<vpd::ParserInterface> getVpdParserInstance();
51
52 /**
53 * @brief Update keyword value.
54 *
55 * This API is used to update keyword value on the EEPROM path and its
56 * redundant path(s) if any taken from system config JSON. And also updates
57 * keyword value on DBus.
58 *
59 * To update IPZ type VPD, input parameter for writing should be in the form
60 * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}).
61 *
62 * To update Keyword type VPD, input parameter for writing should be in the
63 * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}).
64 *
65 * @param[in] i_paramsToWriteData - Input details.
66 *
67 * @return On success returns number of bytes written, on failure returns
68 * -1.
69 */
70 int updateVpdKeyword(const types::WriteVpdParams& i_paramsToWriteData);
71
72 /**
73 * @brief Update keyword value on hardware.
74 *
75 * This API is used to update keyword value on the hardware path.
76 *
77 * To update IPZ type VPD, input parameter for writing should be in the form
78 * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}).
79 *
80 * To update Keyword type VPD, input parameter for writing should be in the
81 * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}).
82 *
83 * @param[in] i_paramsToWriteData - Input details.
84 *
85 * @return On success returns number of bytes written, on failure returns
86 * -1.
87 */
88 int updateVpdKeywordOnHardware(
89 const types::WriteVpdParams& i_paramsToWriteData);
90
91 private:
92 /**
93 * @brief Update keyword value on redundant path.
94 *
95 * This API is used to update keyword value on the given redundant path(s).
96 *
97 * To update IPZ type VPD, input parameter for writing should be in the form
98 * of (Record, Keyword, Value). Eg: ("VINI", "SN", {0x01, 0x02, 0x03}).
99 *
100 * To update Keyword type VPD, input parameter for writing should be in the
101 * form of (Keyword, Value). Eg: ("PE", {0x01, 0x02, 0x03}).
102 *
103 * @param[in] i_fruPath - Redundant EEPROM path.
104 * @param[in] i_paramsToWriteData - Input details.
105 *
106 * @return On success returns number of bytes written, on failure returns
107 * -1.
108 */
109 int updateVpdKeywordOnRedundantPath(
110 const std::string& i_fruPath,
111 const types::WriteVpdParams& i_paramsToWriteData);
112
113 // holds offfset to VPD if applicable.
114 size_t m_vpdStartOffset = 0;
115
116 // Path to the VPD file
117 const std::string& m_vpdFilePath;
118
119 // Path to configuration file, can be empty.
120 nlohmann::json m_parsedJson;
121
122 // Vector to hold VPD.
123 types::BinaryVector m_vpdVector;
124
125}; // parser
126} // namespace vpd