Skip to content

Commit b1e5ac9

Browse files
authored
Merge pull request #599 from souvik1914581/sr_vpdToolDecoratorAsset
vpd-tool dumpObject: show Decorator.Asset properties
2 parents 826a9a7 + 1af7644 commit b1e5ac9

File tree

4 files changed

+85
-14
lines changed

4 files changed

+85
-14
lines changed

vpd-tool/include/tool_types.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ using TableInputData = std::vector<TableRowData>;
6767
// A table column name-size pair
6868
using TableColumnNameSizePair = std::pair<std::string, std::size_t>;
6969

70+
/* Map<Property, Value>*/
71+
using PropertyMap = std::map<std::string, DbusVariantType>;
72+
7073
enum UserOption
7174
{
7275
Exit,

vpd-tool/include/tool_utils.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,56 @@ inline types::DbusVariantType readDbusProperty(const std::string& i_serviceName,
7373
return l_propertyValue;
7474
}
7575

76+
/**
77+
* @brief An API to get property map for an interface.
78+
*
79+
* This API returns a map of property and its value with respect to a particular
80+
* interface.
81+
*
82+
* Note: It will be caller's responsibility to check for empty map returned and
83+
* generate appropriate error.
84+
*
85+
* @param[in] i_service - Service name.
86+
* @param[in] i_objectPath - object path.
87+
* @param[in] i_interface - Interface, for the properties to be listed.
88+
*
89+
* @return - A map of property and value of an interface, if success.
90+
* if failed, empty map.
91+
*/
92+
inline types::PropertyMap
93+
getPropertyMap(const std::string& i_service,
94+
const std::string& i_objectPath,
95+
const std::string& i_interface) noexcept
96+
{
97+
types::PropertyMap l_propertyValueMap;
98+
if (i_service.empty() || i_objectPath.empty() || i_interface.empty())
99+
{
100+
// TODO: Enable logging when verbose is enabled.
101+
// std::cout << "Invalid parameters to get property map" << std::endl;
102+
return l_propertyValueMap;
103+
}
104+
105+
try
106+
{
107+
auto l_bus = sdbusplus::bus::new_default();
108+
auto l_method =
109+
l_bus.new_method_call(i_service.c_str(), i_objectPath.c_str(),
110+
"org.freedesktop.DBus.Properties", "GetAll");
111+
l_method.append(i_interface);
112+
auto l_result = l_bus.call(l_method);
113+
l_result.read(l_propertyValueMap);
114+
}
115+
catch (const sdbusplus::exception::SdBusError& l_ex)
116+
{
117+
// TODO: Enable logging when verbose is enabled.
118+
// std::cerr << "Failed to get property map for service: [" << i_service
119+
// << "], object path: [" << i_objectPath
120+
// << "] Error : " << l_ex.what() << std::endl;
121+
}
122+
123+
return l_propertyValueMap;
124+
}
125+
76126
/**
77127
* @brief An API to print json data on stdout.
78128
*

vpd-tool/include/vpd_tool.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class VpdTool
3838
* If FRU's "Present" property is false, this API returns an empty JSON.
3939
* Note: The caller of this API should handle empty JSON.
4040
*
41-
* @throw json::exception
41+
* @throw json::exception, std::out_of_range, std::bad_alloc
4242
*/
4343
nlohmann::json getFruProperties(const std::string& i_objectPath) const;
4444

vpd-tool/src/vpd_tool.cpp

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ int VpdTool::dumpObject(std::string i_fruPath) const noexcept
115115
catch (std::exception& l_ex)
116116
{
117117
// TODO: Enable logging when verbose is enabled.
118-
// std::cerr << "Dump Object failed for FRU [" << i_fruPath
119-
// << "], Error: " << l_ex.what() << std::endl;
118+
std::cerr << "Dump Object failed for FRU [" << i_fruPath
119+
<< "], Error: " << l_ex.what() << std::endl;
120120
}
121121
return l_rc;
122122
}
@@ -131,9 +131,15 @@ nlohmann::json VpdTool::getFruProperties(const std::string& i_objectPath) const
131131

132132
nlohmann::json l_fruJson = nlohmann::json::object_t({});
133133

134-
l_fruJson.emplace(i_objectPath, nlohmann::json::object_t({}));
134+
// need to trim out the base inventory path in the FRU JSON.
135+
const std::string l_displayObjectPath =
136+
(i_objectPath.find(constants::baseInventoryPath) == std::string::npos)
137+
? i_objectPath
138+
: i_objectPath.substr(strlen(constants::baseInventoryPath));
135139

136-
auto& l_fruObject = l_fruJson[i_objectPath];
140+
l_fruJson.emplace(l_displayObjectPath, nlohmann::json::object_t({}));
141+
142+
auto& l_fruObject = l_fruJson[l_displayObjectPath];
137143

138144
const auto l_prettyNameInJson = getInventoryPropertyJson<std::string>(
139145
i_objectPath, constants::inventoryItemInf, "PrettyName");
@@ -151,15 +157,6 @@ nlohmann::json VpdTool::getFruProperties(const std::string& i_objectPath) const
151157
l_locationCodeInJson.cend());
152158
}
153159

154-
const auto l_subModelInJson = getInventoryPropertyJson<std::string>(
155-
i_objectPath, constants::assetInf, "SubModel");
156-
157-
if (!l_subModelInJson.empty() &&
158-
!l_subModelInJson.value("SubModel", "").empty())
159-
{
160-
l_fruObject.insert(l_subModelInJson.cbegin(), l_subModelInJson.cend());
161-
}
162-
163160
// Get the properties under VINI interface.
164161

165162
nlohmann::json l_viniPropertiesInJson = nlohmann::json::object({});
@@ -184,6 +181,24 @@ nlohmann::json VpdTool::getFruProperties(const std::string& i_objectPath) const
184181
l_fruObject.insert(l_viniPropertiesInJson.cbegin(),
185182
l_viniPropertiesInJson.cend());
186183
}
184+
// if a FRU doesn't have VINI properties, we need to get the properties from
185+
// Decorator.Asset interface
186+
else
187+
{
188+
// Get properties under Decorator.Asset interface
189+
const auto l_decoratorAssetPropertiesMap =
190+
utils::getPropertyMap(constants::inventoryManagerService,
191+
i_objectPath, constants::assetInf);
192+
193+
for (const auto& l_aProperty : l_decoratorAssetPropertiesMap)
194+
{
195+
if (const auto l_propertyValueStr =
196+
std::get_if<std::string>(&l_aProperty.second))
197+
{
198+
l_fruObject.emplace(l_aProperty.first, *l_propertyValueStr);
199+
}
200+
}
201+
}
187202

188203
const auto l_typePropertyJson = getFruTypeProperty(i_objectPath);
189204
if (!l_typePropertyJson.empty())
@@ -192,6 +207,9 @@ nlohmann::json VpdTool::getFruProperties(const std::string& i_objectPath) const
192207
l_typePropertyJson.cend());
193208
}
194209

210+
// insert FRU "TYPE"
211+
l_fruObject.emplace("TYPE", "FRU");
212+
195213
return l_fruJson;
196214
}
197215

0 commit comments

Comments
 (0)