Remove findStringName/Handle functions
Use the `find` functions in BiosStringTable.
Tested: all the code changes are about building bios tables.
tested on fp5280g2, with json
https://gist.github.com/wangzqbj/b24558331cb35d14fca3b555ef03e458
saw the built tables are as expected.
$ pldmtool bios GetBIOSTable -t 0
...
...
PLDM StringTable:
BIOSStringHandle : BIOSString
0 : CodeUpdatePolicy
1 : Concurrent
2 : Disruptive
3 : Led
4 : Model
5 : OUTLET
6 : Off
7 : On
8 : str_example3
$ pldmtool bios GetBIOSTable -t 1
...
...
PLDM AttributeTable:
AttributeHandle: 0, AttributeNameHandle: 0(CodeUpdatePolicy)
AttributeType: BIOSEnumerationReadOnly
NumberOfPossibleValues: 2
PossibleValueStringHandle[0] = 1(Concurrent)
PossibleValueStringHandle[1] = 2(Disruptive)
NumberOfDefaultValues: 1
DefaultValueStringHandleIndex[0] = 0, StringHandle = 1(Concurrent)
AttributeHandle: 1, AttributeNameHandle: 3(Led)
AttributeType: BIOSEnumeration
NumberOfPossibleValues: 2
PossibleValueStringHandle[0] = 7(On)
PossibleValueStringHandle[1] = 6(Off)
NumberOfDefaultValues: 1
DefaultValueStringHandleIndex[0] = 1, StringHandle = 6(Off)
AttributeHandle: 2, AttributeNameHandle: 5(OUTLET)
AttributeType: BIOSInteger
LowerBound: 0
UpperBound: 68002
ScalarIncrement: 1
DefaultValue: 0
AttributeHandle: 3, AttributeNameHandle: 4(Model)
AttributeType: BIOSString
StringType: 0x01
MinimumStringLength: 1
MaximumStringLength: 100
DefaultStringLength: 8
DefaultString: FP5280G2
AttributeHandle: 4, AttributeNameHandle: 8(str_example3)
AttributeType: BIOSStringReadOnly
StringType: 0x00
MinimumStringLength: 1
MaximumStringLength: 100
DefaultStringLength: 2
DefaultString: ef
$ pldmtool bios GetBIOSTable -t 2
...
...
PLDM AttributeValueTable:
AttributeHandle: 0
AttributeType: BIOSEnumerationReadOnly
NumberOfCurrentValues: 1
CurrentValueStringHandleIndex[0] = 0, StringHandle = 1(Concurrent)
AttributeHandle: 1
AttributeType: BIOSEnumeration
NumberOfCurrentValues: 1
CurrentValueStringHandleIndex[0] = 1, StringHandle = 6(Off)
AttributeHandle: 2
AttributeType: BIOSInteger
CurrentValue: 0
AttributeHandle: 3
AttributeType: BIOSString
CurrentStringLength: 12
CurrentString: powersupply0
AttributeHandle: 4
AttributeType: BIOSStringReadOnly
CurrentStringLength: 2
CurrentString: ef
Signed-off-by: John Wang <wangzqbj@inspur.com>
Change-Id: Iba775caffcdfa51d780cd9c211572d3a359b80b4
diff --git a/libpldmresponder/bios.cpp b/libpldmresponder/bios.cpp
index 61b5e49..3f379cb 100644
--- a/libpldmresponder/bios.cpp
+++ b/libpldmresponder/bios.cpp
@@ -299,59 +299,6 @@
return response;
}
-/** @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
- * @return - uint16_t - handle of the string
- */
-StringHandle findStringHandle(const std::string& name,
- const BIOSTable& biosStringTable)
-{
- Table table;
- biosStringTable.load(table);
- auto stringEntry = pldm_bios_table_string_find_by_string(
- table.data(), table.size(), name.c_str());
- if (stringEntry == nullptr)
- {
- std::cerr << "Reached end of BIOS string table,did not find the "
- << "handle for the string, STRING=" << name.c_str() << "\n";
- throw InternalFailure();
- }
-
- return pldm_bios_table_string_entry_decode_handle(stringEntry);
-}
-
-/** @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
- *
- * @return - std::string - name of the corresponding BIOS string
- */
-std::string findStringName(StringHandle stringHdl,
- const BIOSTable& biosStringTable)
-{
- Table table;
- biosStringTable.load(table);
- auto stringEntry = pldm_bios_table_string_find_by_handle(
- table.data(), table.size(), stringHdl);
- if (stringEntry == nullptr)
- {
- std::cerr << "Reached end of BIOS string table,did not find "
- << "string name for handle, STRING_HANDLE=" << stringHdl
- << "\n";
- throw InternalFailure();
- }
- auto strLength =
- pldm_bios_table_string_entry_decode_string_length(stringEntry);
- std::vector<char> buffer(strLength + 1);
- pldm_bios_table_string_entry_decode_string(stringEntry, buffer.data(),
- buffer.size());
-
- return std::string(buffer.data(), buffer.data() + strLength);
-}
-
namespace bios_type_enum
{
@@ -371,7 +318,7 @@
*/
std::vector<uint8_t> findStrIndices(PossibleValuesByHandle possiVals,
CurrentValues currVals,
- const BIOSTable& biosStringTable)
+ const BIOSStringTable& biosStringTable)
{
std::vector<uint8_t> stringIndices;
@@ -380,9 +327,9 @@
StringHandle curHdl;
try
{
- curHdl = findStringHandle(currVal, biosStringTable);
+ curHdl = biosStringTable.findHandle(currVal);
}
- catch (InternalFailure& e)
+ catch (const std::exception& e)
{
std::cerr << "Exception fetching handle for the string, STRING="
<< currVal.c_str() << "\n";
@@ -437,7 +384,8 @@
* @param[in,out] attributeTable - the attribute table
*
*/
-void constructAttrTable(const BIOSTable& biosStringTable, Table& attributeTable)
+void constructAttrTable(const BIOSStringTable& biosStringTable,
+ Table& attributeTable)
{
const auto& attributeMap = getValues();
StringHandle strHandle;
@@ -446,9 +394,9 @@
{
try
{
- strHandle = findStringHandle(key, biosStringTable);
+ strHandle = biosStringTable.findHandle(key);
}
- catch (InternalFailure& e)
+ catch (const std::exception& e)
{
std::cerr << "Could not find handle for BIOS string, ATTRIBUTE="
<< key.c_str() << "\n";
@@ -463,10 +411,10 @@
{
try
{
- auto hdl = findStringHandle(elem, biosStringTable);
+ auto hdl = biosStringTable.findHandle(elem);
possiValsByHdl.push_back(std::move(hdl));
}
- catch (InternalFailure& e)
+ catch (const std::exception& e)
{
std::cerr << "Could not find handle for BIOS string, STRING="
<< elem.c_str() << "\n";
@@ -494,7 +442,7 @@
void constructAttrValueEntry(
const struct pldm_bios_attr_table_entry* attrTableEntry,
- const std::string& attrName, const BIOSTable& biosStringTable,
+ const std::string& attrName, const BIOSStringTable& biosStringTable,
Table& attrValueTable)
{
CurrentValues currVals;
@@ -541,7 +489,8 @@
* @param[in,out] attributeTable - the attribute table
*
*/
-void constructAttrTable(const BIOSTable& biosStringTable, Table& attributeTable)
+void constructAttrTable(const BIOSStringTable& biosStringTable,
+ Table& attributeTable)
{
const auto& attributeMap = getValues();
StringHandle strHandle;
@@ -549,9 +498,9 @@
{
try
{
- strHandle = findStringHandle(key, biosStringTable);
+ strHandle = biosStringTable.findHandle(key);
}
- catch (InternalFailure& e)
+ catch (const std::exception& e)
{
std::cerr << "Could not find handle for BIOS string, ATTRIBUTE="
<< key.c_str() << "\n";
@@ -576,7 +525,7 @@
void constructAttrValueEntry(const pldm_bios_attr_table_entry* attrTableEntry,
const std::string& attrName,
- const BIOSTable& biosStringTable,
+ const BIOSStringTable& biosStringTable,
Table& attrValueTable)
{
std::ignore = biosStringTable;
@@ -616,7 +565,8 @@
* @param[in,out] attributeTable - the attribute table
*
*/
-void constructAttrTable(const BIOSTable& biosStringTable, Table& attributeTable)
+void constructAttrTable(const BIOSStringTable& biosStringTable,
+ Table& attributeTable)
{
const auto& attributeMap = getValues();
StringHandle strHandle;
@@ -624,9 +574,9 @@
{
try
{
- strHandle = findStringHandle(key, biosStringTable);
+ strHandle = biosStringTable.findHandle(key);
}
- catch (InternalFailure& e)
+ catch (const std::exception& e)
{
std::cerr << "Could not find handle for BIOS string, ATTRIBUTE="
<< key.c_str() << "\n";
@@ -650,7 +600,7 @@
void constructAttrValueEntry(const pldm_bios_attr_table_entry* attrTableEntry,
const std::string& attrName,
- const BIOSTable& biosStringTable,
+ const BIOSStringTable& biosStringTable,
Table& attrValueTable)
{
std::ignore = biosStringTable;
@@ -699,7 +649,7 @@
}
}
-using typeHandler = std::function<void(const BIOSTable& biosStringTable,
+using typeHandler = std::function<void(const BIOSStringTable& biosStringTable,
Table& attributeTable)>;
std::map<BIOSJsonName, typeHandler> attrTypeHandlers{
{bios_parser::bIOSEnumJson, bios_type_enum::constructAttrTable},
@@ -715,7 +665,7 @@
* @param[in] request - Request message
*/
Response getBIOSAttributeTable(BIOSTable& biosAttributeTable,
- const BIOSTable& biosStringTable,
+ const BIOSStringTable& biosStringTable,
const char* biosJsonDir, const pldm_msg* request)
{
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES,
@@ -778,7 +728,7 @@
using AttrValTableEntryConstructHandler =
std::function<void(const struct pldm_bios_attr_table_entry* tableEntry,
const std::string& attrName,
- const BIOSTable& biosStringTable, Table& table)>;
+ const BIOSStringTable& biosStringTable, Table& table)>;
using AttrType = uint8_t;
const std::map<AttrType, AttrValTableEntryConstructHandler>
@@ -795,18 +745,21 @@
void constructAttrValueTableEntry(
const struct pldm_bios_attr_table_entry* attrEntry,
- const BIOSTable& biosStringTable, Table& attributeValueTable)
+ const BIOSStringTable& biosStringTable, Table& attributeValueTable)
{
- auto attrName = findStringName(attrEntry->string_handle, biosStringTable);
- if (attrName.empty())
+ auto stringHandle =
+ pldm_bios_table_attr_entry_decode_string_handle(attrEntry);
+ try
{
- std::cerr << "invalid string handle, STRING_HANDLE="
- << attrEntry->string_handle << "\n";
- return;
+ auto attrName = biosStringTable.findString(stringHandle);
+ AttrValTableConstructMap.at(attrEntry->attr_type)(
+ attrEntry, attrName, biosStringTable, attributeValueTable);
}
-
- AttrValTableConstructMap.at(attrEntry->attr_type)(
- attrEntry, attrName, biosStringTable, attributeValueTable);
+ catch (const std::exception& e)
+ {
+ std::cerr << "constructAttrValueTableEntry Error: " << e.what()
+ << std::endl;
+ }
}
/** @brief Construct the BIOS attribute value table
@@ -818,7 +771,7 @@
*/
Response getBIOSAttributeValueTable(BIOSTable& biosAttributeValueTable,
const BIOSTable& biosAttributeTable,
- const BIOSTable& biosStringTable,
+ const BIOSStringTable& biosStringTable,
const pldm_msg* request)
{
Response response(sizeof(pldm_msg_hdr) + PLDM_GET_BIOS_TABLE_MIN_RESP_BYTES,
diff --git a/libpldmresponder/bios_table.cpp b/libpldmresponder/bios_table.cpp
index cf1ec77..9b7dd49 100644
--- a/libpldmresponder/bios_table.cpp
+++ b/libpldmresponder/bios_table.cpp
@@ -46,12 +46,14 @@
stream.read(reinterpret_cast<char*>(response.data() + currSize), fileSize);
}
-BIOSStringTable::BIOSStringTable(const char* filePath) : BIOSTable(filePath)
+BIOSStringTable::BIOSStringTable(const Table& stringTable) :
+ stringTable(stringTable)
{
- if (!isEmpty())
- {
- load(stringTable);
- }
+}
+
+BIOSStringTable::BIOSStringTable(const BIOSTable& biosTable)
+{
+ biosTable.load(stringTable);
}
std::string BIOSStringTable::findString(uint16_t handle) const
@@ -71,6 +73,18 @@
return std::string(buffer.data(), buffer.data() + strLength);
}
+uint16_t BIOSStringTable::findHandle(const std::string& name) const
+{
+ auto stringEntry = pldm_bios_table_string_find_by_string(
+ stringTable.data(), stringTable.size(), name.c_str());
+ if (stringEntry == nullptr)
+ {
+ throw std::invalid_argument("Invalid String Name");
+ }
+
+ return pldm_bios_table_string_entry_decode_handle(stringEntry);
+}
+
} // namespace bios
} // namespace responder
} // namespace pldm
diff --git a/libpldmresponder/bios_table.hpp b/libpldmresponder/bios_table.hpp
index b99b7a6..67503df 100644
--- a/libpldmresponder/bios_table.hpp
+++ b/libpldmresponder/bios_table.hpp
@@ -71,22 +71,37 @@
fs::path filePath;
};
-class BIOSStringTable : public BIOSTable
+class BIOSStringTable
{
public:
- /** @brief Ctor - set file path to persist BIOS String table
+ /** @brief Constructs BIOSStringTable
*
- * @param[in] filePath - file where BIOS table should be persisted
+ * @param[in] stringTable - The stringTable in RAM
*/
- BIOSStringTable(const char* filePath);
+ BIOSStringTable(const Table& stringTable);
+
+ /** @brief Constructs BIOSStringTable
+ *
+ * @param[in] biosTable - The BIOSTable
+ */
+ BIOSStringTable(const BIOSTable& biosTable);
/** @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
+ * @return name of the corresponding BIOS string
+ * @throw std::invalid_argument if the string can not be found.
*/
std::string findString(uint16_t handle) const;
+ /** @brief Find the string handle from the BIOS string table by the given
+ * name
+ * @param[in] name - name of the BIOS string
+ * @return handle of the string
+ * @throw std::invalid_argument if the string can not be found
+ */
+ uint16_t findHandle(const std::string& name) const;
+
private:
Table stringTable;
};