blob: ebb79179c77e6b2cc31ddd154ea633643763d65a [file] [log] [blame]
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05301#include "vpd_tool_impl.hpp"
2
3#include <CLI/CLI.hpp>
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +05304#include <filesystem>
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05305#include <fstream>
6#include <iostream>
7
8using namespace CLI;
9using namespace std;
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053010namespace fs = std::filesystem;
11using namespace openpower::vpd;
12using json = nlohmann::json;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053013
14int main(int argc, char** argv)
15{
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053016 int rc = 0;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053017 App app{"VPD Command line tool to dump the inventory and to read and "
18 "update the keywords"};
19
20 string objectPath{};
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053021 string recordName{};
22 string keyword{};
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053023 string val{};
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053024
25 auto object =
26 app.add_option("--object, -O", objectPath, "Enter the Object Path");
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053027 auto record =
28 app.add_option("--record, -R", recordName, "Enter the Record Name");
29 auto kw = app.add_option("--keyword, -K", keyword, "Enter the Keyword");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053030 auto valOption = app.add_option(
31 "--value, -V", val,
32 "Enter the value. The value to be updated should be either in ascii or "
33 "in hex. ascii eg: 01234; hex eg: 0x30313233");
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053034 auto dumpObjFlag =
35 app.add_flag("--dumpObject, -o",
36 "Dump the given object from the inventory. { "
37 "vpd-tool-exe --dumpObject/-o --object/-O object-name }")
38 ->needs(object);
39
40 auto dumpInvFlag = app.add_flag(
41 "--dumpInventory, -i", "Dump all the inventory objects. { vpd-tool-exe "
42 "--dumpInventory/-i }");
43
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053044 auto readFlag =
45 app.add_flag("--readKeyword, -r",
46 "Read the data of the given keyword. { "
47 "vpd-tool-exe --readKeyword/-r --object/-O "
48 "\"object-name\" --record/-R \"record-name\" --keyword/-K "
49 "\"keyword-name\" }")
50 ->needs(object)
51 ->needs(record)
52 ->needs(kw);
53
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053054 auto writeFlag =
55 app.add_flag(
56 "--writeKeyword, -w, --updateKeyword, -u",
57 "Update the value. { vpd-tool-exe "
58 "--writeKeyword/-w/--updateKeyword/-u "
59 "--object/-O object-name --record/-R record-name --keyword/-K "
60 "keyword-name --value/-V value-to-be-updated }")
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050061 ->needs(object)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053062 ->needs(record)
63 ->needs(kw)
64 ->needs(valOption);
65
PriyangaRamasamy0407b172020-03-31 13:57:18 +053066 auto forceResetFlag = app.add_flag(
67 "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe "
68 "--forceReset/-f/-F }");
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053069 auto Hardware = app.add_flag(
70 "--Hardware, -H",
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050071 "This is a supplementary flag to write directly to hardware. When the "
72 "-H flag is given, User should provide valid hardware/eeprom path (and "
73 "not dbus object path) in the -O/--object path.");
PriyangaRamasamy0407b172020-03-31 13:57:18 +053074
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053075 CLI11_PARSE(app, argc, argv);
76
Santosh Puranik0246a4d2020-11-04 16:57:39 +053077 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053078 auto jsObject = json::parse(inventoryJson);
79
80 try
81 {
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053082 if (*Hardware)
83 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050084 if (!fs::exists(objectPath)) // if dbus object path is given or
85 // invalid eeprom path is given
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053086 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -050087 string errorMsg = "Invalid EEPROM path : ";
88 errorMsg += objectPath;
89 errorMsg +=
90 ". The given EEPROM path doesn't exist. Provide valid "
91 "EEPROM path when -H flag is used. Refer help option. ";
92 throw runtime_error(errorMsg);
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053093 }
94 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053095 if (*dumpObjFlag)
96 {
97 VpdTool vpdToolObj(move(objectPath));
98 vpdToolObj.dumpObject(jsObject);
99 }
100
101 else if (*dumpInvFlag)
102 {
103 VpdTool vpdToolObj;
104 vpdToolObj.dumpInventory(jsObject);
105 }
106
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530107 else if (*readFlag)
108 {
109 VpdTool vpdToolObj(move(objectPath), move(recordName),
110 move(keyword));
111 vpdToolObj.readKeyword();
112 }
113
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530114 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530115 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500116 VpdTool vpdToolObj(move(objectPath), move(recordName),
117 move(keyword), move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530118 rc = vpdToolObj.updateKeyword();
119 }
120
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530121 else if (*forceResetFlag)
122 {
123 VpdTool vpdToolObj;
124 vpdToolObj.forceReset(jsObject);
125 }
126
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530127 else if (*writeFlag && *Hardware)
128 {
PriyangaRamasamy83ea53f2021-05-13 05:55:21 -0500129 VpdTool vpdToolObj(move(objectPath), move(recordName),
130 move(keyword), move(val));
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530131 rc = vpdToolObj.updateHardware();
132 }
133
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530134 else
135 {
136 throw runtime_error("One of the valid options is required. Refer "
137 "--help for list of options.");
138 }
139 }
140
Patrick Williams8e15b932021-10-06 13:04:22 -0500141 catch (const exception& e)
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530142 {
143 cerr << e.what();
144 }
145
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530146 return rc;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530147}