FruDevice: use uint8_t instead of char for data
Currently there is mix of char and uint8_t in binary data handling code,
which require lot of unnecessary casts.
This change replaces char by uint8_t everywhere and removes casts where
they are not needed anymore.
Tested: tested that there is no changes in FRU data published to dbus
Signed-off-by: Andrei Kartashev <a.kartashev@yadro.com>
Change-Id: Ic1ee984299c9be790a661e0c8e2111b3a2b98d6b
diff --git a/src/FruDevice.cpp b/src/FruDevice.cpp
index 93f114b..76da2d3 100644
--- a/src/FruDevice.cpp
+++ b/src/FruDevice.cpp
@@ -69,7 +69,7 @@
static constexpr std::array<const char*, 5> FRU_AREAS = {
"INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"};
const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]");
-using DeviceMap = boost::container::flat_map<int, std::vector<char>>;
+using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>;
using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
static std::set<size_t> busBlacklist;
@@ -114,9 +114,9 @@
// Read and validate FRU contents, given the flag required for raw i2c, the open
// file handle, a read method, and a helper string for failures.
-std::vector<char> readFruContents(int flag, int file, uint16_t address,
- ReadBlockFunc readBlock,
- const std::string& errorHelp)
+std::vector<uint8_t> readFruContents(int flag, int file, uint16_t address,
+ ReadBlockFunc readBlock,
+ const std::string& errorHelp)
{
std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
@@ -137,7 +137,7 @@
return {};
}
- std::vector<char> device;
+ std::vector<uint8_t> device;
device.insert(device.end(), blockData.begin(), blockData.begin() + 8);
bool hasMultiRecords = false;
@@ -145,7 +145,7 @@
for (size_t jj = 1; jj <= FRU_AREAS.size(); jj++)
{
// Offset value can be 255.
- int areaOffset = static_cast<uint8_t>(device[jj]);
+ int areaOffset = device[jj];
if (areaOffset == 0)
{
continue;
@@ -178,7 +178,7 @@
{
// device[area count] is the index to the last area because the 0th
// entry is not an offset in the common header.
- int areaOffset = static_cast<uint8_t>(device[FRU_AREAS.size()]);
+ int areaOffset = device[FRU_AREAS.size()];
areaOffset *= 8;
// the multi-area record header is 5 bytes long.
@@ -454,7 +454,7 @@
// TODO: This code is very similar to the non-eeprom version and can be merged
// with some tweaks.
-static std::vector<char> processEeprom(int bus, int address)
+static std::vector<uint8_t> processEeprom(int bus, int address)
{
auto path = getEepromPath(bus, address);
@@ -467,7 +467,7 @@
std::string errorMessage = "eeprom at " + std::to_string(bus) +
" address " + std::to_string(address);
- std::vector<char> device = readFruContents(
+ std::vector<uint8_t> device = readFruContents(
0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
close(file);
@@ -525,7 +525,7 @@
// contents, but we found it.
foundList.insert(address);
- std::vector<char> device = processEeprom(i2cBus, address);
+ std::vector<uint8_t> device = processEeprom(i2cBus, address);
if (!device.empty())
{
devices->emplace(address, device);
@@ -625,7 +625,7 @@
std::string errorMessage =
"bus " + std::to_string(bus) + " address " + std::to_string(ii);
- std::vector<char> device =
+ std::vector<uint8_t> device =
readFruContents(flag, file, static_cast<uint16_t>(ii),
readBlockData, errorMessage);
if (device.empty())
@@ -850,8 +850,8 @@
* iterator is no longer usable.
*/
static std::pair<DecodeState, std::string>
- decodeFruData(std::vector<char>::const_iterator& iter,
- const std::vector<char>::const_iterator& end)
+ decodeFruData(std::vector<uint8_t>::const_iterator& iter,
+ const std::vector<uint8_t>::const_iterator& end)
{
std::string value;
unsigned int i;
@@ -895,7 +895,7 @@
value = std::string();
for (i = 0; i < len; i++, iter++)
{
- uint8_t val = static_cast<uint8_t>(*iter);
+ uint8_t val = *iter;
value.push_back(bcdPlusToChar(val >> 4));
value.push_back(bcdPlusToChar(val & 0xf));
}
@@ -908,9 +908,7 @@
value = std::string();
for (i = 0; i < len; i++, iter++)
{
- /* we need to cast to unsigned before the promotion to
- * int, so we don't sign-extend. */
- accum |= static_cast<unsigned char>(*iter) << accumBitLen;
+ accum |= *iter << accumBitLen;
accumBitLen += 8;
while (accumBitLen >= 6)
{
@@ -926,14 +924,14 @@
return make_pair(DecodeState::ok, value);
}
-bool formatFru(const std::vector<char>& fruBytes,
+bool formatFru(const std::vector<uint8_t>& fruBytes,
boost::container::flat_map<std::string, std::string>& result)
{
if (fruBytes.size() <= 8)
{
return false;
}
- std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
+ std::vector<uint8_t>::const_iterator fruAreaOffsetField = fruBytes.begin();
result["Common_Format_Version"] =
std::to_string(static_cast<int>(*fruAreaOffsetField));
@@ -951,7 +949,7 @@
if (offset > 1)
{
// +2 to skip format and length
- std::vector<char>::const_iterator fruBytesIter =
+ std::vector<uint8_t>::const_iterator fruBytesIter =
fruBytes.begin() + offset + 2;
if (fruBytesIter >= fruBytes.end())
@@ -1052,14 +1050,13 @@
{
throw std::invalid_argument("Invalid Address.");
}
- std::vector<uint8_t>& ret =
- reinterpret_cast<std::vector<uint8_t>&>(device->second);
+ std::vector<uint8_t>& ret = device->second;
return ret;
}
void AddFruObjectToDbus(
- std::vector<char>& device,
+ std::vector<uint8_t>& device,
boost::container::flat_map<
std::pair<size_t, size_t>,
std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
@@ -1204,7 +1201,7 @@
iface->initialize();
}
-static bool readBaseboardFru(std::vector<char>& baseboardFru)
+static bool readBaseboardFru(std::vector<uint8_t>& baseboardFru)
{
// try to read baseboard fru from file
std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
@@ -1214,7 +1211,8 @@
size_t fileSize = static_cast<size_t>(baseboardFruFile.tellg());
baseboardFru.resize(fileSize);
baseboardFruFile.seekg(0, std::ios_base::beg);
- baseboardFruFile.read(baseboardFru.data(), fileSize);
+ baseboardFruFile.read(reinterpret_cast<char*>(baseboardFru.data()),
+ fileSize);
}
else
{
@@ -1232,7 +1230,7 @@
return false;
}
// verify legal fru by running it through fru parsing logic
- if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
+ if (!formatFru(fru, tmp))
{
std::cerr << "Invalid fru format during writeFru\n";
return false;
@@ -1436,10 +1434,10 @@
UNKNOWN_BUS_OBJECT_COUNT = 0;
// todo, get this from a more sensable place
- std::vector<char> baseboardFru;
+ std::vector<uint8_t> baseboardFru;
if (readBaseboardFru(baseboardFru))
{
- boost::container::flat_map<int, std::vector<char>>
+ boost::container::flat_map<int, std::vector<uint8_t>>
baseboardDev;
baseboardDev.emplace(0, baseboardFru);
busmap[0] = std::make_shared<DeviceMap>(baseboardDev);