blob: 1ca50b51bc86f815a49f89c29ca9f7912722da29 [file] [log] [blame]
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05301#include "config.h"
2
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05303#include "editor_impl.hpp"
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05304#include "types.hpp"
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05305#include "utils.hpp"
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05306
7#include <nlohmann/json.hpp>
PriyangaRamasamy887a42a2020-09-03 00:33:57 +05308#include <string>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05309
10using json = nlohmann::json;
11
12class VpdTool
13{
14 private:
15 const std::string fruPath;
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053016 const std::string recordName;
17 const std::string keyword;
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053018 const std::string value;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053019
Alpana Kumarib6965f12020-06-01 00:32:21 -050020 // Store Type of FRU
21 std::string fruType;
22
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053023 /**
24 * @brief Debugger
25 * Displays the output in JSON.
26 *
27 * @param[in] output - json output to be displayed
28 */
29 void debugger(json output);
30
31 /**
32 * @brief make Dbus Call
33 *
34 * @param[in] objectName - dbus Object
35 * @param[in] interface - dbus Interface
36 * @param[in] kw - keyword under the interface
37 *
38 * @return dbus call response
39 */
40 auto makeDBusCall(const std::string& objectName,
41 const std::string& interface, const std::string& kw);
42
43 /**
44 * @brief Adds FRU type and Location Code
45 * Appends the type of the FRU and location code to the output
46 *
47 * @param[in] exIntf - extraInterfaces json from INVENTORY_JSON
48 * @param[in] object - The D-Bus object to read the location code from
49 * @param[out] kwVal - JSON object into which the FRU type and location code
50 * are placed
51 */
52 void addFruTypeAndLocation(json exIntf, const std::string& object,
53 json& kwVal);
54
55 /**
56 * @brief Get VINI properties
57 * Making a dbus call for properties [SN, PN, CC, FN, DR]
58 * under VINI interface.
59 *
60 * @param[in] invPath - Value of inventory Path
61 * @param[in] exIntf - extraInterfaces json from INVENTORY_JSON
62 *
63 * @return json output which gives the properties under invPath's VINI
64 * interface
65 */
66 json getVINIProperties(std::string invPath, json exIntf);
67
68 /**
69 * @brief Get ExtraInterface Properties
70 * Making a dbus call for those properties under extraInterfaces.
71 *
72 * @param[in] invPath - Value of inventory path
73 * @param[in] extraInterface - One of the invPath's extraInterfaces whose
74 * value is not null
75 * @param[in] prop - All properties of the extraInterface.
76 *
77 * @return json output which gives the properties under invPath's
78 * extraInterface.
79 */
80 void getExtraInterfaceProperties(std::string invPath,
81 std::string extraInterface, json prop,
82 json exIntf, json& output);
83
84 /**
85 * @brief Interface Decider
86 * Decides whether to make the dbus call for
87 * getting properites from extraInterface or from
88 * VINI interface, depending on the value of
89 * extraInterfaces object in the inventory json.
90 *
91 * @param[in] itemEEPROM - holds the reference of one of the EEPROM objects.
92 *
93 * @return json output for one of the EEPROM objects.
94 */
95 json interfaceDecider(json& itemEEPROM);
96
97 /**
98 * @brief Parse Inventory JSON
99 * Parses the complete inventory json and depending upon
100 * the user option makes the dbuscall for the frus
101 * via interfaceDecider function.
102 *
103 * @param[in] jsObject - Inventory json object
104 * @param[in] flag - flag which tells about the user option(either
105 * dumpInventory or dumpObject)
106 * @param[in] fruPath - fruPath is empty for dumpInventory option and holds
107 * valid fruPath for dumpObject option.
108 *
109 * @return output json
110 */
111 json parseInvJson(const json& jsObject, char flag, std::string fruPath);
112
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530113 /**
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530114 * @brief eraseInventoryPath
115 * Remove the INVENTORY_PATH - "/xyz/openbmc_project/inventory"
116 * for code convenience.
117 *
118 * @param[out] fru - Reference to the fru path whose INVENTORY_PATH is
119 * stripped off.
120 */
121 void eraseInventoryPath(std::string& fru);
122
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530123 /**
124 * @brief printReturnCode
125 * Prints the return code of the program in console output, whenever
126 * the program fails to exit successfully.
127 *
128 * @param[in] returnCode - return code of the program.
129 */
PriyangaRamasamy6ee637a2021-02-12 04:49:02 -0600130 void printReturnCode(int returnCode);
131
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530132 /**
133 * @brief Convert hex/ascii values to Binary
134 * @param[in] - value in hex/ascii.
135 * @param[out] - value in binary.
136 */
137 openpower::vpd::Binary toBinary(const std::string& value);
138
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530139 public:
140 /**
141 * @brief Dump the complete inventory in JSON format
142 *
143 * @param[in] jsObject - Inventory JSON specified in configure file.
144 */
145 void dumpInventory(const nlohmann::basic_json<>& jsObject);
146
147 /**
148 * @brief Dump the given inventory object in JSON format
149 *
150 * @param[in] jsObject - Inventory JSON specified in configure file.
151 */
152 void dumpObject(const nlohmann::basic_json<>& jsObject);
153
154 /**
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530155 * @brief Read keyword
156 * Read the given object path, record name and keyword
157 * from the inventory and display the value of the keyword
158 * in JSON format.
159 */
160 void readKeyword();
161
162 /**
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530163 * @brief Update Keyword
164 * Update the given keyword with the given value.
165 *
166 * @return return code (Success(0)/Failure(-1))
167 */
168 int updateKeyword();
169
170 /**
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530171 * @brief Force Reset
172 * Clearing the inventory cache data and restarting the
173 * phosphor inventory manager and also retriggering all the
174 * udev events.
175 *
176 * @param[in] jsObject - Inventory JSON specified in configure file.
177 */
178 void forceReset(const nlohmann::basic_json<>& jsObject);
179
180 /**
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530181 * @brief Get Printable Value
182 *
183 * Checks if the vector value has non printable characters.
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530184 * Returns hex value if non printable char is found else
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530185 * returns ascii value.
186 *
187 * @param[in] vector - Reference of the Binary vector
188 * @return printable value - either in hex or in ascii.
189 */
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530190 std::string getPrintableValue(const std::vector<unsigned char>& vector);
191
192 /**
193 * @brief Update Hardware
194 * If the given record-keyword pair is present in dbus_properties.json,
195 * then will update the given data in both dbus and hardware.
196 * Else update the given data only in hardware.
197 * @return returncode (success/failure).
198 */
199 int updateHardware();
PriyangaRamasamy887a42a2020-09-03 00:33:57 +0530200
201 /**
PriyangaRamasamycdf943c2020-03-18 02:25:30 +0530202 * @brief Constructor
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530203 * Constructor is called during the
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530204 * object instantiation for dumpInventory option and
205 * forceReset option.
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530206 */
207 VpdTool()
208 {
209 }
210
211 /**
212 * @brief Constructor
213 * Constructor is called during the
214 * object instantiation for dumpObject option.
215 */
216 VpdTool(const std::string&& fru) : fruPath(std::move(fru))
217 {
218 }
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530219
220 /**
221 * @brief Constructor
222 * Constructor is called during the
223 * object instantiation for readKeyword option.
224 */
225 VpdTool(const std::string&& fru, const std::string&& recName,
226 const std::string&& kw) :
227 fruPath(std::move(fru)),
228 recordName(std::move(recName)), keyword(std::move(kw))
229 {
230 }
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530231
232 /**
233 * @brief Constructor
234 * Constructor is called during the
235 * object instantiation for updateKeyword option.
236 */
237
238 VpdTool(const std::string&& fru, const std::string&& recName,
239 const std::string&& kw, const std::string&& val) :
240 fruPath(std::move(fru)),
241 recordName(std::move(recName)), keyword(std::move(kw)),
242 value(std::move(val))
243 {
244 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530245};