SimpleEventBuilder/fragment_dataformat.h

57 lines
2.0 KiB
C++

#include <cstddef>
#include <cstdint>
#include <cstring>
#define START_OF_HEADER_MARKER 0xee1234ee
typedef struct Header {
uint32_t startOfHeaderMarker = START_OF_HEADER_MARKER;
uint32_t headerSize;
uint32_t fragmentSize;
uint32_t sourceIdentifier;
uint32_t runNumber;
uint32_t detectorEventNumber;
uint32_t numberOfStatusElements;
uint32_t *statusElementsArray;
/*friend std::ostream& operator <<(std::ostream& os, Header const& header)
{
return os << std::setw(8) << std::setfill('0') << header.startOfHeaderMarker << '\n'
<< std::setw(8) << std::setfill('0') << header.headerSize << '\n'
<< std::setw(8) << std::setfill('0') << header.fragmentSize << '\n'
<< std::setw(8) << std::setfill('0') << header.runNumber << '\n'
<< std::setw(8) << std::setfill('0') << header.detectorEventNumber << '\n'
<< std::setw(8) << std::setfill('0') << header.numberOfStatusElements << '\n';
}*/
} Header;
typedef struct Fragment {
Header header;
uint32_t *payloadElements;
} Fragment;
enum ERROR_CODES {
INCORRECT_ERROR = (1 << 0),
CORRUPTED_ERROR = (1 << 1),
MISSING_DATA_ERROR = (1 << 2),
TIMEOUT_ERROR = (1 << 3)
};
void encode_header(uint32_t *buffer, const Header &header) {
buffer[0] = header.startOfHeaderMarker;
buffer[1] = header.headerSize;
buffer[2] = header.fragmentSize;
buffer[3] = header.sourceIdentifier;
buffer[4] = header.runNumber;
buffer[5] = header.detectorEventNumber;
buffer[6] = header.numberOfStatusElements;
std::memcpy(&buffer[7], header.statusElementsArray, header.numberOfStatusElements * sizeof(uint32_t));
}
void encode_fragment(uint32_t *buffer, const Fragment &fragment){
encode_header(buffer, fragment.header);
std::memcpy(&buffer[fragment.header.headerSize], fragment.payloadElements, (fragment.header.fragmentSize - fragment.header.headerSize) * sizeof(uint32_t));
}