Skip to content

Commit 47e30cc

Browse files
author
Souvik Roy
committed
Implement vpd-tool dumpObject stub (ibm-openbmc#501)
This commit implements vpd-tool --dumpObject stub and some associated utility methods and constants for the same. This commit allows user to provide --dumpObject command line option to vpd-tool. Note: This commit does not include actual implementation of --dumpObject. Change-Id: I821777237a3080d390e2bba151cd685ef8994bf9 Signed-off-by: Souvik Roy <[email protected]>
1 parent bcbb9ae commit 47e30cc

File tree

7 files changed

+157
-3
lines changed

7 files changed

+157
-3
lines changed

vpd-tool/include/tool_constants.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@ constexpr auto ipzVpdInfPrefix = "com.ibm.ipzvpd.";
2020
constexpr auto vpdManagerService = "com.ibm.VPD.Manager";
2121
constexpr auto vpdManagerObjectPath = "/com/ibm/VPD/Manager";
2222
constexpr auto vpdManagerInfName = "com.ibm.VPD.Manager";
23+
constexpr auto inventoryItemInf = "xyz.openbmc_project.Inventory.Item";
24+
constexpr auto kwdVpdInf = "com.ibm.ipzvpd.VINI";
25+
constexpr auto locationCodeInf = "com.ibm.ipzvpd.Location";
26+
constexpr auto assetInf = "xyz.openbmc_project.Inventory.Decorator.Asset";
2327
} // namespace constants
2428
} // namespace vpd
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#pragma once
2+
3+
#include <nlohmann/json.hpp>
4+
5+
#include <fstream>
6+
7+
namespace vpd
8+
{
9+
namespace jsonUtility
10+
{
11+
12+
/**
13+
* @brief API to parse respective JSON.
14+
*
15+
* Exception is thrown in case of JSON parse error.
16+
*
17+
* @param[in] i_pathToJson - Path to JSON.
18+
* @return Parsed JSON.
19+
* @throw std::runtime_error
20+
*/
21+
inline nlohmann::json getParsedJson(const std::string& i_pathToJson)
22+
{
23+
if (i_pathToJson.empty())
24+
{
25+
throw std::runtime_error("Path to JSON is missing");
26+
}
27+
28+
try
29+
{
30+
if (!std::filesystem::exists(i_pathToJson) ||
31+
std::filesystem::is_empty(i_pathToJson))
32+
{
33+
throw std::runtime_error("Incorrect File Path or empty file = " +
34+
i_pathToJson);
35+
}
36+
}
37+
catch (std::exception& l_ex)
38+
{
39+
throw std::runtime_error("Failed to check file path: " + i_pathToJson +
40+
" Error: " + l_ex.what());
41+
}
42+
43+
std::ifstream l_jsonFile(i_pathToJson);
44+
if (!l_jsonFile)
45+
{
46+
throw std::runtime_error("Failed to access Json path = " +
47+
i_pathToJson);
48+
}
49+
50+
try
51+
{
52+
return nlohmann::json::parse(l_jsonFile);
53+
}
54+
catch (const nlohmann::json::parse_error& l_ex)
55+
{
56+
throw std::runtime_error("Failed to parse JSON file: " + i_pathToJson +
57+
" Error: " + l_ex.what());
58+
}
59+
}
60+
61+
} // namespace jsonUtility
62+
} // namespace vpd

vpd-tool/include/tool_utils.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ inline types::DbusVariantType readDbusProperty(const std::string& i_serviceName,
7575
* @brief An API to print json data on stdout.
7676
*
7777
* @param[in] i_jsonData - JSON object.
78+
*
79+
* @throw std::runtime_error
7880
*/
7981
inline void printJson(const nlohmann::json& i_jsonData)
8082
{

vpd-tool/include/vpd_tool.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <nlohmann/json.hpp>
4+
35
#include <string>
46

57
namespace vpd
@@ -17,6 +19,27 @@ namespace vpd
1719
*/
1820
class VpdTool
1921
{
22+
private:
23+
/**
24+
* @brief Get specific properties of a FRU in JSON format.
25+
*
26+
* For a given Object Path of a FRU, this API returns the following
27+
* properties of the FRU in JSON format:
28+
* - Present property, Pretty Name, Location Code, Sub Model
29+
* - SN, PN, CC, FN, DR keywords under VINI record.
30+
* - type and TYPE of the FRU
31+
*
32+
* @param[in] i_fruPath - DBus object path
33+
*
34+
* @return On success, returns the properties of the FRU in JSON format,
35+
* otherwise returns an empty JSON object.
36+
* Note: The caller of this API should
37+
* check for empty JSON object
38+
*
39+
* @throw std::runtime_error
40+
*/
41+
nlohmann::json getFruPropertiesJson(const std::string& i_fruPath) const;
42+
2043
public:
2144
/**
2245
* @brief Read keyword value.
@@ -39,5 +62,20 @@ class VpdTool
3962
const std::string& i_recordName,
4063
const std::string& i_keywordName, const bool i_onHardware,
4164
const std::string& i_fileToSave = {});
65+
66+
/**
67+
* @brief Dump the given inventory object in JSON format to console.
68+
*
69+
* For a given Object Path of a FRU, this API dumps the following properties
70+
* of the FRU in JSON format to console:
71+
* - Present property, Pretty Name, Location Code, Sub Model
72+
* - SN, PN, CC, FN, DR keywords under VINI record.
73+
* - type and TYPE of the FRU
74+
*
75+
* @param[in] i_fruPath - DBus object path.
76+
*
77+
* @return On success returns 0, otherwise returns -1.
78+
*/
79+
int dumpObject(const std::string& i_fruPath) const noexcept;
4280
};
4381
} // namespace vpd

vpd-tool/meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ sources = ['src/vpd_tool_main.cpp',
1313

1414
vpd_tool_exe = executable('vpd-tool',
1515
sources,
16-
include_directories : ['include/'],
16+
include_directories : ['include/','../'],
1717
dependencies: dependency_list,
1818
install: true
1919
)

vpd-tool/src/vpd_tool.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
#include "config.h"
2+
13
#include "vpd_tool.hpp"
24

35
#include "tool_constants.hpp"
6+
#include "tool_json_utility.hpp"
47
#include "tool_types.hpp"
58
#include "tool_utils.hpp"
69

@@ -74,4 +77,36 @@ int VpdTool::readKeyword(const std::string& i_vpdPath,
7477
}
7578
return l_rc;
7679
}
80+
81+
int VpdTool::dumpObject(const std::string& i_fruPath) const noexcept
82+
{
83+
int l_rc{constants::FAILURE};
84+
try
85+
{
86+
const nlohmann::json l_resultJson = getFruPropertiesJson(i_fruPath);
87+
if (!l_resultJson.empty())
88+
{
89+
utils::printJson(l_resultJson);
90+
l_rc = constants::SUCCESS;
91+
}
92+
}
93+
catch (std::exception& l_ex)
94+
{
95+
std::cerr << "Dump Object failed for FRU: " << i_fruPath
96+
<< " Error: " << l_ex.what() << std::endl;
97+
}
98+
return l_rc;
99+
}
100+
101+
nlohmann::json VpdTool::getFruPropertiesJson(const std::string& i_fruPath) const
102+
{
103+
nlohmann::json l_resultInJson = nlohmann::json::array({});
104+
const nlohmann::json& l_sysCfgJsonObj =
105+
vpd::jsonUtility::getParsedJson(INVENTORY_JSON_SYM_LINK);
106+
// TODO: Implement getFruPropertiesJson
107+
(void)i_fruPath;
108+
(void)l_sysCfgJsonObj;
109+
return l_resultInJson;
110+
}
111+
77112
} // namespace vpd

vpd-tool/src/vpd_tool_main.cpp

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "tool_constants.hpp"
2+
#include "tool_json_utility.hpp"
3+
#include "tool_utils.hpp"
24
#include "vpd_tool.hpp"
35

46
#include <CLI/CLI.hpp>
@@ -8,7 +10,7 @@
810

911
int main(int argc, char** argv)
1012
{
11-
int l_rc = -1;
13+
int l_rc = vpd::constants::FAILURE;
1214
CLI::App l_app{"VPD Command Line Tool"};
1315

1416
std::string l_vpdPath{};
@@ -26,7 +28,10 @@ int main(int argc, char** argv)
2628
" From hardware to console: "
2729
"vpd-tool -r -H -O <DBus Object Path> -R <Record Name> -K <Keyword Name>\n"
2830
" From hardware to file: "
29-
"vpd-tool -r -H -O <EEPROM Path> -R <Record Name> -K <Keyword Name> --file <File Path>");
31+
"vpd-tool -r -H -O <EEPROM Path> -R <Record Name> -K <Keyword Name> --file <File Path>\n"
32+
"Dump Object:\n"
33+
" From dbus to console: "
34+
"vpd-tool -o -O <DBus Object Path>");
3035

3136
auto l_objectOption = l_app.add_option("--object, -O", l_vpdPath,
3237
"File path");
@@ -46,6 +51,9 @@ int main(int argc, char** argv)
4651
auto l_hardwareFlag = l_app.add_flag("--Hardware, -H",
4752
"CAUTION: Developer only option.");
4853

54+
auto l_dumpObjFlag = l_app.add_flag("--dumpObject, -o", "Dump Object")
55+
->needs(l_objectOption);
56+
4957
// ToDo: Take offset value from user for hardware path.
5058

5159
CLI11_PARSE(l_app, argc, argv);
@@ -100,6 +108,11 @@ int main(int argc, char** argv)
100108
l_rc = l_vpdToolObj.readKeyword(l_vpdPath, l_recordName, l_keywordName,
101109
l_isHardwareOperation, l_filePath);
102110
}
111+
else if (!l_dumpObjFlag->empty())
112+
{
113+
vpd::VpdTool l_vpdToolObj;
114+
l_rc = l_vpdToolObj.dumpObject(l_vpdPath);
115+
}
103116
else
104117
{
105118
std::cout << l_app.help() << std::endl;

0 commit comments

Comments
 (0)