VpdTool:Treat NULL char at the end as a terminator
If there is a non-printable null character at the end or if
there are many non printable null characters towards the end of
the buffer, vpd-tool treats null char(s) as a null terminator
and displays the bytes in ascii format.
vpd-tool displays the vector data in hex if,
- If there are any other non printable character(s) except null.
- If the non printable null character(s) is at the middle of the
buffer.
Test:
Tested on rainier
-> vpd-tool output before emplacing this logic:(displays SN in hex)
(where SN has only one non printable NULL at the end)
root@rain135bmc:/tmp# vpd-tool -o -O /system/chassis/motherboard/powersupply3
[
{
"/system/chassis/motherboard/powersupply3": {
"CC": "2B1E",
"DR": "IBM PS",
"FN": "03FP302",
"LocationCode": "U78DB.ND0.WZS000G-E3",
"PN": "03FP301",
"SN": "0x594c31304b593038434b364b00",
"TYPE": "FRU",
"type": "xyz.openbmc_project.Inventory.Item.PowerSupply"
}
}
]
-> vpd-tool output after emplacing this logic:(displays SN in string format)
(by treating end NULL char as NULL terminator)
root@rain135bmc:/tmp# ./vpd-tool -o -O "/system/chassis/motherboard/powersupply3"
[
{
"/system/chassis/motherboard/powersupply3": {
"CC": "2B1E",
"DR": "IBM PS",
"FN": "03FP302",
"LocationCode": "U78DB.ND0.WZS000G-E3",
"PN": "03FP301",
"SN": "YL10KY08CK6K",
"TYPE": "FRU",
"type": "xyz.openbmc_project.Inventory.Item.PowerSupply"
}
}
]
Signed-off-by: Priyanga Ramasamy <priyanga24@in.ibm.com>
Change-Id: I4fb073c49ed3cad117e8dbd0d4da54dcb35f0dc2
diff --git a/ibm_vpd_utils.cpp b/ibm_vpd_utils.cpp
index bf50bd8..42bb99a 100644
--- a/ibm_vpd_utils.cpp
+++ b/ibm_vpd_utils.cpp
@@ -570,32 +570,41 @@
return kwVal;
}
-string getPrintableValue(const vector<unsigned char>& vec)
+string byteArrayToHexString(const Binary& vec)
+{
+ stringstream ss;
+ string hexRep = "0x";
+ ss << hexRep;
+ string str = ss.str();
+
+ // convert Decimal to Hex string
+ for (auto& v : vec)
+ {
+ ss << setfill('0') << setw(2) << hex << (int)v;
+ str = ss.str();
+ }
+ return str;
+}
+
+string getPrintableValue(const Binary& vec)
{
string str{};
- bool printableChar = true;
- for (auto i : vec)
- {
- if (!isprint(i))
- {
- printableChar = false;
- break;
- }
- }
- if (!printableChar)
- {
- stringstream ss;
- string hexRep = "0x";
- ss << hexRep;
- str = ss.str();
+ // find for a non printable value in the vector
+ const auto it = std::find_if(vec.begin(), vec.end(),
+ [](const auto& ele) { return !isprint(ele); });
- // convert Decimal to Hex
- for (auto& v : vec)
+ if (it != vec.end()) // if the given vector has any non printable value
+ {
+ for (auto itr = it; itr != vec.end(); itr++)
{
- ss << setfill('0') << setw(2) << hex << (int)v;
- str = ss.str();
+ if (*itr != 0x00)
+ {
+ str = byteArrayToHexString(vec);
+ return str;
+ }
}
+ str = string(vec.begin(), it);
}
else
{