libpldmresponder: Add BIOSStringTable
s/BIOSStringTable/biosStringTable/g, make the variable begin
with a lowercase letter.
Added a BIOSStringTable class and implemented a command
method(find string by handle).
Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: I1e8fbf97025f68a87246e8661d9fdb14cbb62ecf
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index b0b12d8..082ac0f 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -224,16 +224,16 @@
/** @brief Construct the BIOS string table
*
- * @param[in,out] BIOSStringTable - the string table
+ * @param[in,out] biosStringTable - the string table
* @param[in] request - Request message
*/
-Response getBIOSStringTable(BIOSTable& BIOSStringTable, const pldm_msg* request)
+Response getBIOSStringTable(BIOSTable& biosStringTable, const pldm_msg* request)
{
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES,
0);
auto responsePtr = reinterpret_cast<pldm_msg*>(response.data());
- if (!BIOSStringTable.isEmpty())
+ if (!biosStringTable.isEmpty())
{
auto rc = encode_get_bios_table_resp(
request->hdr.instance_id, PLDM_SUCCESS,
@@ -245,7 +245,7 @@
return CmdHandler::ccOnlyResponse(request, rc);
}
- BIOSStringTable.load(response);
+ biosStringTable.load(response);
return response;
}
auto biosStrings = bios_parser::getStrings();
@@ -278,7 +278,7 @@
}
pldm::responder::utils::padAndChecksum(stringTable);
- BIOSStringTable.store(stringTable);
+ biosStringTable.store(stringTable);
response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES +
stringTable.size(),
0);
@@ -297,14 +297,14 @@
/** @brief Find the string handle from the BIOS string table given the name
*
* @param[in] name - name of the BIOS string
- * @param[in] BIOSStringTable - the string table
+ * @param[in] biosStringTable - the string table
* @return - uint16_t - handle of the string
*/
StringHandle findStringHandle(const std::string& name,
- const BIOSTable& BIOSStringTable)
+ const BIOSTable& biosStringTable)
{
Table table;
- BIOSStringTable.load(table);
+ biosStringTable.load(table);
auto stringEntry = pldm_bios_table_string_find_by_string(
table.data(), table.size(), name.c_str());
if (stringEntry == nullptr)
@@ -320,15 +320,15 @@
/** @brief Find the string name from the BIOS string table for a string handle
*
* @param[in] stringHdl - string handle
- * @param[in] BIOSStringTable - the string table
+ * @param[in] biosStringTable - the string table
*
* @return - std::string - name of the corresponding BIOS string
*/
std::string findStringName(StringHandle stringHdl,
- const BIOSTable& BIOSStringTable)
+ const BIOSTable& biosStringTable)
{
Table table;
- BIOSStringTable.load(table);
+ biosStringTable.load(table);
auto stringEntry = pldm_bios_table_string_find_by_handle(
table.data(), table.size(), stringHdl);
if (stringEntry == nullptr)
@@ -359,14 +359,14 @@
* values for an attribute
* @param[in] currVals - vector of strings comprising all current values
* for an attribute
- * @param[in] BIOSStringTable - the string table
+ * @param[in] biosStringTable - the string table
*
* @return - std::vector<uint8_t> - indices into the array of the possible
* values of string handles
*/
std::vector<uint8_t> findStrIndices(PossibleValuesByHandle possiVals,
CurrentValues currVals,
- const BIOSTable& BIOSStringTable)
+ const BIOSTable& biosStringTable)
{
std::vector<uint8_t> stringIndices;
@@ -375,7 +375,7 @@
StringHandle curHdl;
try
{
- curHdl = findStringHandle(currVal, BIOSStringTable);
+ curHdl = findStringHandle(currVal, biosStringTable);
}
catch (InternalFailure& e)
{
@@ -426,12 +426,12 @@
/** @brief Construct the attibute table for BIOS type Enumeration and
* Enumeration ReadOnly
- * @param[in] BIOSStringTable - the string table
+ * @param[in] biosStringTable - the string table
* @param[in] biosJsonDir - path where the BIOS json files are present
* @param[in,out] attributeTable - the attribute table
*
*/
-void constructAttrTable(const BIOSTable& BIOSStringTable, Table& attributeTable)
+void constructAttrTable(const BIOSTable& biosStringTable, Table& attributeTable)
{
const auto& attributeMap = getValues();
StringHandle strHandle;
@@ -440,7 +440,7 @@
{
try
{
- strHandle = findStringHandle(key, BIOSStringTable);
+ strHandle = findStringHandle(key, biosStringTable);
}
catch (InternalFailure& e)
{
@@ -461,7 +461,7 @@
{
try
{
- auto hdl = findStringHandle(elem, BIOSStringTable);
+ auto hdl = findStringHandle(elem, biosStringTable);
possiValsByHdl.push_back(std::move(hdl));
}
catch (InternalFailure& e)
@@ -492,7 +492,7 @@
void constructAttrValueEntry(
const struct pldm_bios_attr_table_entry* attrTableEntry,
- const std::string& attrName, const BIOSTable& BIOSStringTable,
+ const std::string& attrName, const BIOSTable& biosStringTable,
Table& attrValueTable)
{
CurrentValues currVals;
@@ -513,7 +513,7 @@
pvHdls.data(), pv_num);
std::sort(currVals.begin(), currVals.end());
- auto currValStrIndices = findStrIndices(pvHdls, currVals, BIOSStringTable);
+ auto currValStrIndices = findStrIndices(pvHdls, currVals, biosStringTable);
auto entryLength = pldm_bios_table_attr_value_entry_encode_enum_length(
currValStrIndices.size());
@@ -534,12 +534,12 @@
/** @brief Construct the attibute table for BIOS type String and
* String ReadOnly
- * @param[in] BIOSStringTable - the string table
+ * @param[in] biosStringTable - the string table
* @param[in] biosJsonDir - path where the BIOS json files are present
* @param[in,out] attributeTable - the attribute table
*
*/
-void constructAttrTable(const BIOSTable& BIOSStringTable, Table& attributeTable)
+void constructAttrTable(const BIOSTable& biosStringTable, Table& attributeTable)
{
const auto& attributeMap = getValues();
StringHandle strHandle;
@@ -547,7 +547,7 @@
{
try
{
- strHandle = findStringHandle(key, BIOSStringTable);
+ strHandle = findStringHandle(key, biosStringTable);
}
catch (InternalFailure& e)
{
@@ -574,10 +574,10 @@
void constructAttrValueEntry(const pldm_bios_attr_table_entry* attrTableEntry,
const std::string& attrName,
- const BIOSTable& BIOSStringTable,
+ const BIOSTable& biosStringTable,
Table& attrValueTable)
{
- std::ignore = BIOSStringTable;
+ std::ignore = biosStringTable;
std::string currStr;
uint16_t currStrLen = 0;
try
@@ -610,11 +610,11 @@
/** @brief Construct the attibute table for BIOS type Integer and
* Integer ReadOnly
- * @param[in] BIOSStringTable - the string table
+ * @param[in] biosStringTable - the string table
* @param[in,out] attributeTable - the attribute table
*
*/
-void constructAttrTable(const BIOSTable& BIOSStringTable, Table& attributeTable)
+void constructAttrTable(const BIOSTable& biosStringTable, Table& attributeTable)
{
const auto& attributeMap = getValues();
StringHandle strHandle;
@@ -622,7 +622,7 @@
{
try
{
- strHandle = findStringHandle(key, BIOSStringTable);
+ strHandle = findStringHandle(key, biosStringTable);
}
catch (InternalFailure& e)
{
@@ -648,10 +648,10 @@
void constructAttrValueEntry(const pldm_bios_attr_table_entry* attrTableEntry,
const std::string& attrName,
- const BIOSTable& BIOSStringTable,
+ const BIOSTable& biosStringTable,
Table& attrValueTable)
{
- std::ignore = BIOSStringTable;
+ std::ignore = biosStringTable;
uint64_t currentValue;
try
{
@@ -697,7 +697,7 @@
}
}
-using typeHandler = std::function<void(const BIOSTable& BIOSStringTable,
+using typeHandler = std::function<void(const BIOSTable& biosStringTable,
Table& attributeTable)>;
std::map<BIOSJsonName, typeHandler> attrTypeHandlers{
{bios_parser::bIOSEnumJson, bios_type_enum::constructAttrTable},
@@ -707,13 +707,13 @@
/** @brief Construct the BIOS attribute table
*
- * @param[in,out] BIOSAttributeTable - the attribute table
- * @param[in] BIOSStringTable - the string table
+ * @param[in,out] biosAttributeTable - the attribute table
+ * @param[in] biosStringTable - the string table
* @param[in] biosJsonDir - path where the BIOS json files are present
* @param[in] request - Request message
*/
-Response getBIOSAttributeTable(BIOSTable& BIOSAttributeTable,
- const BIOSTable& BIOSStringTable,
+Response getBIOSAttributeTable(BIOSTable& biosAttributeTable,
+ const BIOSTable& biosStringTable,
const char* biosJsonDir, const pldm_msg* request)
{
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES,
@@ -722,7 +722,7 @@
uint32_t nxtTransferHandle = 0;
uint8_t transferFlag = PLDM_START_AND_END;
- if (BIOSAttributeTable.isEmpty())
+ if (biosAttributeTable.isEmpty())
{ // no persisted table, constructing fresh table and response
Table attributeTable;
fs::path dir(biosJsonDir);
@@ -733,7 +733,7 @@
fs::path file = dir / it->first;
if (fs::exists(file))
{
- it->second(BIOSStringTable, attributeTable);
+ it->second(biosStringTable, attributeTable);
}
}
@@ -743,7 +743,7 @@
PLDM_BIOS_TABLE_UNAVAILABLE);
}
pldm::responder::utils::padAndChecksum(attributeTable);
- BIOSAttributeTable.store(attributeTable);
+ biosAttributeTable.store(attributeTable);
response.resize(sizeof(pldm_msg_hdr) +
PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES +
attributeTable.size());
@@ -767,7 +767,7 @@
{
return CmdHandler::ccOnlyResponse(request, rc);
}
- BIOSAttributeTable.load(response);
+ biosAttributeTable.load(response);
}
return response;
@@ -776,7 +776,7 @@
using AttrValTableEntryConstructHandler =
std::function<void(const struct pldm_bios_attr_table_entry* tableEntry,
const std::string& attrName,
- const BIOSTable& BIOSStringTable, Table& table)>;
+ const BIOSTable& biosStringTable, Table& table)>;
using AttrType = uint8_t;
const std::map<AttrType, AttrValTableEntryConstructHandler>
@@ -793,9 +793,9 @@
void constructAttrValueTableEntry(
const struct pldm_bios_attr_table_entry* attrEntry,
- const BIOSTable& BIOSStringTable, Table& attributeValueTable)
+ const BIOSTable& biosStringTable, Table& attributeValueTable)
{
- auto attrName = findStringName(attrEntry->string_handle, BIOSStringTable);
+ auto attrName = findStringName(attrEntry->string_handle, biosStringTable);
if (attrName.empty())
{
std::cerr << "invalid string handle, STRING_HANDLE="
@@ -804,19 +804,19 @@
}
AttrValTableConstructMap.at(attrEntry->attr_type)(
- attrEntry, attrName, BIOSStringTable, attributeValueTable);
+ attrEntry, attrName, biosStringTable, attributeValueTable);
}
/** @brief Construct the BIOS attribute value table
*
- * @param[in,out] BIOSAttributeValueTable - the attribute value table
- * @param[in] BIOSAttributeTable - the attribute table
- * @param[in] BIOSStringTable - the string table
+ * @param[in,out] biosAttributeValueTable - the attribute value table
+ * @param[in] biosAttributeTable - the attribute table
+ * @param[in] biosStringTable - the string table
* @param[in] request - Request message
*/
-Response getBIOSAttributeValueTable(BIOSTable& BIOSAttributeValueTable,
- const BIOSTable& BIOSAttributeTable,
- const BIOSTable& BIOSStringTable,
+Response getBIOSAttributeValueTable(BIOSTable& biosAttributeValueTable,
+ const BIOSTable& biosAttributeTable,
+ const BIOSTable& biosStringTable,
const pldm_msg* request)
{
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES,
@@ -825,7 +825,7 @@
uint32_t nxtTransferHandle = 0;
uint8_t transferFlag = PLDM_START_AND_END;
- if (!BIOSAttributeValueTable.isEmpty())
+ if (!biosAttributeValueTable.isEmpty())
{
auto rc = encode_get_bios_table_resp(
request->hdr.instance_id, PLDM_SUCCESS, nxtTransferHandle,
@@ -836,18 +836,18 @@
return CmdHandler::ccOnlyResponse(request, rc);
}
- BIOSAttributeValueTable.load(response);
+ biosAttributeValueTable.load(response);
return response;
}
Table attributeValueTable;
Table attributeTable;
- BIOSAttributeTable.load(attributeTable);
+ biosAttributeTable.load(attributeTable);
traverseBIOSAttrTable(
attributeTable,
- [&BIOSStringTable, &attributeValueTable](
+ [&biosStringTable, &attributeValueTable](
const struct pldm_bios_attr_table_entry* tableEntry) {
- constructAttrValueTableEntry(tableEntry, BIOSStringTable,
+ constructAttrValueTableEntry(tableEntry, biosStringTable,
attributeValueTable);
});
if (attributeValueTable.empty())
@@ -855,7 +855,7 @@
return CmdHandler::ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE);
}
pldm::responder::utils::padAndChecksum(attributeValueTable);
- BIOSAttributeValueTable.store(attributeValueTable);
+ biosAttributeValueTable.store(attributeValueTable);
response.resize(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES +
attributeValueTable.size());
@@ -897,27 +897,27 @@
fs::path tablesPath(BIOS_TABLES_DIR);
auto stringTablePath = tablesPath / stringTableFile;
- BIOSTable BIOSStringTable(stringTablePath.c_str());
+ BIOSTable biosStringTable(stringTablePath.c_str());
auto attrTablePath = tablesPath / attrTableFile;
- BIOSTable BIOSAttributeTable(attrTablePath.c_str());
- if (BIOSAttributeTable.isEmpty() || BIOSStringTable.isEmpty())
+ BIOSTable biosAttributeTable(attrTablePath.c_str());
+ if (biosAttributeTable.isEmpty() || biosStringTable.isEmpty())
{
return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE);
}
auto attrValueTablePath = tablesPath / attrValTableFile;
- BIOSTable BIOSAttributeValueTable(attrValueTablePath.c_str());
+ BIOSTable biosAttributeValueTable(attrValueTablePath.c_str());
- if (BIOSAttributeValueTable.isEmpty())
+ if (biosAttributeValueTable.isEmpty())
{
Table attributeValueTable;
Table attributeTable;
- BIOSAttributeTable.load(attributeTable);
+ biosAttributeTable.load(attributeTable);
traverseBIOSAttrTable(
attributeTable,
- [&BIOSStringTable, &attributeValueTable](
+ [&biosStringTable, &attributeValueTable](
const struct pldm_bios_attr_table_entry* tableEntry) {
- constructAttrValueTableEntry(tableEntry, BIOSStringTable,
+ constructAttrValueTableEntry(tableEntry, biosStringTable,
attributeValueTable);
});
if (attributeValueTable.empty())
@@ -925,11 +925,11 @@
return ccOnlyResponse(request, PLDM_BIOS_TABLE_UNAVAILABLE);
}
pldm::responder::utils::padAndChecksum(attributeValueTable);
- BIOSAttributeValueTable.store(attributeValueTable);
+ biosAttributeValueTable.store(attributeValueTable);
}
Response table;
- BIOSAttributeValueTable.load(table);
+ biosAttributeValueTable.load(table);
auto entry = pldm_bios_table_attr_value_find_by_handle(
table.data(), table.size(), attributeHandle);
@@ -980,40 +980,40 @@
return CmdHandler::ccOnlyResponse(request, rc);
}
- BIOSTable BIOSStringTable(
+ BIOSTable biosStringTable(
(std::string(biosTablePath) + "/" + stringTableFile).c_str());
- BIOSTable BIOSAttributeTable(
+ BIOSTable biosAttributeTable(
(std::string(biosTablePath) + "/" + attrTableFile).c_str());
- BIOSTable BIOSAttributeValueTable(
+ BIOSTable biosAttributeValueTable(
(std::string(biosTablePath) + "/" + attrValTableFile).c_str());
switch (tableType)
{
case PLDM_BIOS_STRING_TABLE:
- response = getBIOSStringTable(BIOSStringTable, request);
+ response = getBIOSStringTable(biosStringTable, request);
break;
case PLDM_BIOS_ATTR_TABLE:
- if (BIOSStringTable.isEmpty())
+ if (biosStringTable.isEmpty())
{
rc = PLDM_BIOS_TABLE_UNAVAILABLE;
}
else
{
response = getBIOSAttributeTable(
- BIOSAttributeTable, BIOSStringTable, biosJsonDir, request);
+ biosAttributeTable, biosStringTable, biosJsonDir, request);
}
break;
case PLDM_BIOS_ATTR_VAL_TABLE:
- if (BIOSAttributeTable.isEmpty() || BIOSStringTable.isEmpty())
+ if (biosAttributeTable.isEmpty() || biosStringTable.isEmpty())
{
rc = PLDM_BIOS_TABLE_UNAVAILABLE;
}
else
{
- response = getBIOSAttributeValueTable(BIOSAttributeValueTable,
- BIOSAttributeTable,
- BIOSStringTable, request);
+ response = getBIOSAttributeValueTable(biosAttributeValueTable,
+ biosAttributeTable,
+ biosStringTable, request);
}
break;
default:
@@ -1029,8 +1029,8 @@
return response;
}
-} // end namespace internal
-} // namespace bios
+} // namespace internal
+} // namespace bios
} // namespace responder
} // namespace pldm
diff --git a/libpldmresponder/bios_table.cpp b/libpldmresponder/bios_table.cpp
index df2c8dd..cf1ec77 100644
--- a/libpldmresponder/bios_table.cpp
+++ b/libpldmresponder/bios_table.cpp
@@ -2,6 +2,8 @@
#include <fstream>
+#include "bios_table.h"
+
namespace pldm
{
@@ -44,6 +46,31 @@
stream.read(reinterpret_cast<char*>(response.data() + currSize), fileSize);
}
+BIOSStringTable::BIOSStringTable(const char* filePath) : BIOSTable(filePath)
+{
+ if (!isEmpty())
+ {
+ load(stringTable);
+ }
+}
+
+std::string BIOSStringTable::findString(uint16_t handle) const
+{
+ auto stringEntry = pldm_bios_table_string_find_by_handle(
+ stringTable.data(), stringTable.size(), handle);
+ if (stringEntry == nullptr)
+ {
+ throw std::invalid_argument("Invalid String Handle");
+ }
+ auto strLength =
+ pldm_bios_table_string_entry_decode_string_length(stringEntry);
+ std::vector<char> buffer(strLength + 1 /* sizeof '\0' */);
+ pldm_bios_table_string_entry_decode_string(stringEntry, buffer.data(),
+ buffer.size());
+
+ return std::string(buffer.data(), buffer.data() + strLength);
+}
+
} // namespace bios
} // namespace responder
} // namespace pldm
diff --git a/libpldmresponder/bios_table.hpp b/libpldmresponder/bios_table.hpp
index 2b4077c..b99b7a6 100644
--- a/libpldmresponder/bios_table.hpp
+++ b/libpldmresponder/bios_table.hpp
@@ -71,6 +71,26 @@
fs::path filePath;
};
+class BIOSStringTable : public BIOSTable
+{
+ public:
+ /** @brief Ctor - set file path to persist BIOS String table
+ *
+ * @param[in] filePath - file where BIOS table should be persisted
+ */
+ BIOSStringTable(const char* filePath);
+
+ /** @brief Find the string name from the BIOS string table for a string
+ * handle
+ * @param[in] handle - string handle
+ * @return - std::string - name of the corresponding BIOS string
+ */
+ std::string findString(uint16_t handle) const;
+
+ private:
+ Table stringTable;
+};
+
} // namespace bios
} // namespace responder
} // namespace pldm
diff --git a/test/libpldmresponder_bios_test.cpp b/test/libpldmresponder_bios_test.cpp
index 762a960..ebef789 100644
--- a/test/libpldmresponder_bios_test.cpp
+++ b/test/libpldmresponder_bios_test.cpp
@@ -221,7 +221,7 @@
static void SetUpTestCase() // will execute once at the begining of all
// TestAllBIOSTables objects
{
- char tmpdir[] = "/tmp/allBiosTables.XXXXXX";
+ char tmpdir[] = "/tmp/allBIOSTables.XXXXXX";
biosPath = fs::path(mkdtemp(tmpdir));
}
@@ -509,30 +509,30 @@
{ // will be executed before each individual test defined
// in TestSingleTypeBIOSTable
char tmpdir[] = "/tmp/singleTypeBIOSTable.XXXXXX";
- destBiosPath = fs::path(mkdtemp(tmpdir));
+ destBIOSPath = fs::path(mkdtemp(tmpdir));
}
void TearDown() override
{ // will be executed after each individual test
// defined in TestSingleTypeBIOSTable
- fs::remove_all(destBiosPath);
+ fs::remove_all(destBIOSPath);
}
void CopySingleJsonFile(std::string file)
{
fs::path srcDir("./bios_jsons");
- fs::path srcBiosPath = srcDir / file;
- std::filesystem::copy(srcBiosPath, destBiosPath);
+ fs::path srcBIOSPath = srcDir / file;
+ std::filesystem::copy(srcBIOSPath, destBIOSPath);
}
- fs::path destBiosPath;
+ fs::path destBIOSPath;
};
TEST_F(TestSingleTypeBIOSTable, getBIOSAttributeValueTableBasedOnStringTypeTest)
{
// Copy string json file to the destination
TestSingleTypeBIOSTable::CopySingleJsonFile(bios_parser::bIOSStrJson);
- auto fpath = TestSingleTypeBIOSTable::destBiosPath.c_str();
+ auto fpath = TestSingleTypeBIOSTable::destBIOSPath.c_str();
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES>
requestPayload{};
@@ -616,7 +616,7 @@
{
// Copy integer json file to the destination
TestSingleTypeBIOSTable::CopySingleJsonFile(bios_parser::bIOSIntegerJson);
- auto fpath = TestSingleTypeBIOSTable::destBiosPath.c_str();
+ auto fpath = TestSingleTypeBIOSTable::destBIOSPath.c_str();
std::array<uint8_t, sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_REQ_BYTES>
requestPayload{};