xref: /openbmc/openpower-vpd-parser/vpd_tool_impl.hpp (revision 6c71c9dcbf9e56a6ae44525b11ca4e4a92b5ab5e)
1 #include "config.h"
2 
3 #include "editor_impl.hpp"
4 #include "ibm_vpd_utils.hpp"
5 #include "types.hpp"
6 
7 #include <nlohmann/json.hpp>
8 #include <string>
9 
10 using json = nlohmann::json;
11 
12 class VpdTool
13 {
14   private:
15     const std::string fruPath;
16     const std::string recordName;
17     const std::string keyword;
18     const std::string value;
19 
20     // Store Type of FRU
21     std::string fruType;
22 
23     /**
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 
113     /**
114      * @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 
123     /**
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      */
130     void printReturnCode(int returnCode);
131 
132     /**
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 
139   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     /**
155      * @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     /**
163      * @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     /**
171      * @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     /**
181      * @brief Get Printable Value
182      *
183      * Checks if the vector value has non printable characters.
184      * Returns hex value if non printable char is found else
185      * returns ascii value.
186      *
187      * @param[in] vector - Reference of the Binary vector
188      * @return printable value - either in hex or in ascii.
189      */
190     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();
200 
201     /**
202      * @brief Constructor
203      * Constructor is called during the
204      * object instantiation for dumpInventory option and
205      * forceReset option.
206      */
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     }
219 
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     }
231 
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     }
245 };
246