blob: 809f4dcaf55a1182e0b0b4e8206f564ab61a79be [file] [log] [blame] [edit]
#include "dictionary.hpp"
#include "libpldm/utils.h"
#include <stdplus/print.hpp>
Dictionary::Dictionary(uint32_t resourceId) : resourceId(resourceId)
{}
std::span<const uint8_t> Dictionary::getDictionaryBytes()
{
return dictionary;
}
OperationStatus
Dictionary::addToDictionaryBytes(std::span<const uint8_t> payload,
bool hasChecksum)
{
this->dictionary.insert(this->dictionary.end(), payload.begin(),
payload.end());
if (hasChecksum)
{
OperationStatus checksumVerification = verifyChecksum();
// remove the checksum bytes from the dictionary
this->dictionary.erase(this->dictionary.begin() +
(this->dictionary.size() - 4),
this->dictionary.end());
if (checksumVerification == OperationStatus::ChecksumFailure)
{
return checksumVerification;
}
}
return OperationStatus::Success;
}
OperationStatus Dictionary::verifyChecksum()
{
uint32_t payloadLength = this->dictionary.size();
auto calculatedChecksum =
crc32(&(this->dictionary.front()), payloadLength - 4);
uint8_t byte0 = this->dictionary[payloadLength - 4];
uint8_t byte1 = this->dictionary[payloadLength - 3];
uint8_t byte2 = this->dictionary[payloadLength - 2];
uint8_t byte3 = this->dictionary[payloadLength - 1];
uint32_t receivedChecksum =
(byte0 | (byte1 << 8) | (byte2 << 16) | (byte3 << 24));
if (calculatedChecksum == receivedChecksum)
{
if (DEBUG)
{
stdplus::println(stderr,
"Successfully verified checksum in "
"dictionary extraction for resource id: {}"
" with calculated checksum: {} and "
"received checksum: {}",
static_cast<uint32_t>(resourceId),
static_cast<uint32_t>(calculatedChecksum),
static_cast<uint32_t>(receivedChecksum));
}
return OperationStatus::Success;
}
stdplus::println(stderr,
"Failed to verify checksum in "
"dictionary extraction for resource id: {}"
" with calculated checksum: {} and "
"received checksum: {}",
static_cast<uint32_t>(resourceId),
static_cast<uint32_t>(calculatedChecksum),
static_cast<uint32_t>(receivedChecksum));
return OperationStatus::ChecksumFailure;
}
// Getters and Setters
uint32_t Dictionary::getResourceId() const
{
return this->resourceId;
}
// Getter for currentSchemaClass
uint8_t Dictionary::getCurrentSchemaClass() const
{
return this->currentSchemaClass;
}
// Setter for currentSchemaClass
void Dictionary::setCurrentSchemaClass(uint8_t newSchemaClass)
{
this->currentSchemaClass = newSchemaClass;
}
// Getter for currentTransferHandle
uint32_t Dictionary::getCurrentTransferHandle() const
{
return this->currentTransferHandle;
}
// Setter for currentTransferHandle
void Dictionary::setCurrentTransferHandle(uint32_t newTransferHandle)
{
this->currentTransferHandle = newTransferHandle;
}
// Getter for currentTransferOperation
uint8_t Dictionary::getCurrentTransferOperation() const
{
return this->currentTransferOperation;
}
// Setter for currentTransferOperation
void Dictionary::setCurrentTransferOperation(uint8_t newTransferOperation)
{
this->currentTransferOperation = newTransferOperation;
}