blob: a4891f6e2c9befe51f04fa007d0cc88d41bf99d9 [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{};
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053024 string path{};
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053025
26 auto object =
27 app.add_option("--object, -O", objectPath, "Enter the Object Path");
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053028 auto record =
29 app.add_option("--record, -R", recordName, "Enter the Record Name");
30 auto kw = app.add_option("--keyword, -K", keyword, "Enter the Keyword");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053031 auto valOption = app.add_option(
32 "--value, -V", val,
33 "Enter the value. The value to be updated should be either in ascii or "
34 "in hex. ascii eg: 01234; hex eg: 0x30313233");
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053035 auto pathOption =
36 app.add_option("--path, -P", path,
37 "Path - if hardware option is used, give either EEPROM "
38 "path/Object path; if not give the object path");
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053039
40 auto dumpObjFlag =
41 app.add_flag("--dumpObject, -o",
42 "Dump the given object from the inventory. { "
43 "vpd-tool-exe --dumpObject/-o --object/-O object-name }")
44 ->needs(object);
45
46 auto dumpInvFlag = app.add_flag(
47 "--dumpInventory, -i", "Dump all the inventory objects. { vpd-tool-exe "
48 "--dumpInventory/-i }");
49
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053050 auto readFlag =
51 app.add_flag("--readKeyword, -r",
52 "Read the data of the given keyword. { "
53 "vpd-tool-exe --readKeyword/-r --object/-O "
54 "\"object-name\" --record/-R \"record-name\" --keyword/-K "
55 "\"keyword-name\" }")
56 ->needs(object)
57 ->needs(record)
58 ->needs(kw);
59
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053060 auto writeFlag =
61 app.add_flag(
62 "--writeKeyword, -w, --updateKeyword, -u",
63 "Update the value. { vpd-tool-exe "
64 "--writeKeyword/-w/--updateKeyword/-u "
65 "--object/-O object-name --record/-R record-name --keyword/-K "
66 "keyword-name --value/-V value-to-be-updated }")
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053067 ->needs(pathOption)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053068 ->needs(record)
69 ->needs(kw)
70 ->needs(valOption);
71
PriyangaRamasamy0407b172020-03-31 13:57:18 +053072 auto forceResetFlag = app.add_flag(
73 "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe "
74 "--forceReset/-f/-F }");
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053075 auto Hardware = app.add_flag(
76 "--Hardware, -H",
77 "This is a supplementary flag to read/write directly from/to hardware. "
78 "Enter the hardware path while using the object option in "
79 "corresponding read/write flags. This --Hardware flag is to be given "
80 "along with readKeyword/writeKeyword.");
PriyangaRamasamy0407b172020-03-31 13:57:18 +053081
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053082 CLI11_PARSE(app, argc, argv);
83
Santosh Puranik0246a4d2020-11-04 16:57:39 +053084 ifstream inventoryJson(INVENTORY_JSON_SYM_LINK);
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053085 auto jsObject = json::parse(inventoryJson);
86
87 try
88 {
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +053089 if (*Hardware)
90 {
91 if (!fs::exists(path)) // dbus object path
92 {
93 string p = getVpdFilePath(INVENTORY_JSON_SYM_LINK, path);
94 if (p.empty()) // object path not present in inventory json
95 {
96 string errorMsg = "Invalid object path : ";
97 errorMsg += path;
98 errorMsg += ". Unable to find the corresponding EEPROM "
99 "path for the given object path : ";
100 errorMsg += path;
101 errorMsg += " in the vpd inventory json : ";
102 errorMsg += INVENTORY_JSON_SYM_LINK;
103 throw runtime_error(errorMsg);
104 }
105 else
106 {
107 path = p;
108 }
109 }
110 }
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530111 if (*dumpObjFlag)
112 {
113 VpdTool vpdToolObj(move(objectPath));
114 vpdToolObj.dumpObject(jsObject);
115 }
116
117 else if (*dumpInvFlag)
118 {
119 VpdTool vpdToolObj;
120 vpdToolObj.dumpInventory(jsObject);
121 }
122
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +0530123 else if (*readFlag)
124 {
125 VpdTool vpdToolObj(move(objectPath), move(recordName),
126 move(keyword));
127 vpdToolObj.readKeyword();
128 }
129
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530130 else if (*writeFlag && !*Hardware)
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530131 {
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530132 VpdTool vpdToolObj(move(path), move(recordName), move(keyword),
133 move(val));
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530134 rc = vpdToolObj.updateKeyword();
135 }
136
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530137 else if (*forceResetFlag)
138 {
139 VpdTool vpdToolObj;
140 vpdToolObj.forceReset(jsObject);
141 }
142
PriyangaRamasamyc0a534f2020-08-24 21:29:18 +0530143 else if (*writeFlag && *Hardware)
144 {
145 VpdTool vpdToolObj(move(path), move(recordName), move(keyword),
146 move(val));
147 rc = vpdToolObj.updateHardware();
148 }
149
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530150 else
151 {
152 throw runtime_error("One of the valid options is required. Refer "
153 "--help for list of options.");
154 }
155 }
156
157 catch (exception& e)
158 {
159 cerr << e.what();
160 }
161
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530162 return rc;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530163}