Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • bias/daqs/base-daq
1 result
Show changes
Commits on Source (1)
......@@ -16,6 +16,8 @@
#include <cmath>
#include <functional>
#include <optional>
#include <ctime>
#include <iomanip>
namespace inaf::oasbo::PacketLib {
......@@ -32,23 +34,20 @@ protected:
std::unordered_map<uint, std::string> indexToFieldNameMap;
std::function<Structure(std::string)> sourceReadingFunc;
void updateFieldSizes(
const Structure &paramsTuple) {
void updateFieldSizes(const Structure &paramsTuple) {
std::for_each(paramsTuple.begin(), paramsTuple.end(),
[&](const std::tuple<uint, std::string, uint> &tup) {
this->fieldSizes.push_back(std::get<2>(tup));
});
}
void updateFieldOffsets(
const Structure &paramsTuple) {
void updateFieldOffsets(const Structure &paramsTuple) {
uint offset = 0;
for (size_t i = 0; i < paramsTuple.size(); i++) {
indexToOffsetsMap[i] = offset;
offset += std::get<2>(paramsTuple[i]);
}
}
void updateFieldNameAndIndexMap(
const Structure &paramsTuple) {
void updateFieldNameAndIndexMap(const Structure &paramsTuple) {
std::for_each(paramsTuple.begin(), paramsTuple.end(),
[&](const std::tuple<uint, std::string, uint> &tup) {
this->fieldNameToIndexMap[std::get<1>(tup)] = std::get<0>(
......@@ -63,24 +62,27 @@ protected:
updateFieldSizes(structure);
updateFieldOffsets(structure);
updateFieldNameAndIndexMap(structure);
uint bitSize = std::accumulate(fieldSizes.begin(), fieldSizes.end(),0);
this->byteSize = bitSize%8 == 0 ? bitSize/8 : bitSize/8 +1;
uint bitSize = std::accumulate(fieldSizes.begin(), fieldSizes.end(), 0);
this->byteSize = bitSize % 8 == 0 ? bitSize / 8 : bitSize / 8 + 1;
}
public:
virtual ~BasePacketStructure() = default;
BasePacketStructure(std::string source, std::function<Structure(std::string)> sourceReadingFunc) : source(source), sourceReadingFunc(sourceReadingFunc){
BasePacketStructure(std::string source,
std::function<Structure(std::string)> sourceReadingFunc) :
source(source), sourceReadingFunc(sourceReadingFunc) {
this->updateStructure(source);
};
BasePacketStructure(const BasePacketStructure &other){
this->source = other.source;
this->byteSize = other.byteSize;
this->fieldSizes = other.fieldSizes;
this->indexToOffsetsMap = other.indexToOffsetsMap;
this->fieldNameToIndexMap = other.fieldNameToIndexMap;
this->indexToFieldNameMap = other.indexToFieldNameMap;
this->sourceReadingFunc = other.sourceReadingFunc;
}
;
BasePacketStructure(const BasePacketStructure &other) {
this->source = other.source;
this->byteSize = other.byteSize;
this->fieldSizes = other.fieldSizes;
this->indexToOffsetsMap = other.indexToOffsetsMap;
this->fieldNameToIndexMap = other.fieldNameToIndexMap;
this->indexToFieldNameMap = other.indexToFieldNameMap;
this->sourceReadingFunc = other.sourceReadingFunc;
}
void changeSource(std::string source) {
......@@ -96,8 +98,11 @@ public:
if (index <= this->numberOfFields()) {
return indexToOffsetsMap.at(index);
} else {
std::cerr << "No field at " << index << ", max is "
<< numberOfFields() << ", returning nullopt." << std::endl;
time_t now = time(nullptr) ;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "No field at " << index
<< ", max is " << numberOfFields() << ", returning nullopt."
<< std::endl;
return std::nullopt;
}
}
......@@ -105,8 +110,11 @@ public:
if (index <= this->numberOfFields()) {
return this->fieldSizes[index];
} else {
std::cerr << "No field at " << index << ", max is "
<< numberOfFields() << ", returning nullopt." << std::endl;
time_t now = time(nullptr) ;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "No field at " << index
<< ", max is " << numberOfFields() << ", returning nullopt."
<< std::endl;
return std::nullopt;
}
}
......@@ -119,7 +127,9 @@ public:
try {
return this->fieldNameToIndexMap.at(fieldName);
} catch (const std::out_of_range &oor) {
std::cerr << "No field of name " << fieldName
time_t now = time(nullptr) ;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "No field of name " << fieldName
<< ", returning nullopt." << std::endl;
return std::nullopt;
}
......@@ -129,8 +139,11 @@ public:
try {
return this->indexToFieldNameMap.at(index);
} catch (const std::out_of_range &oor) {
std::cerr << "No field at " << index << ", max is "
<< numberOfFields() << ", returning nullopt." << std::endl;
time_t now = time(nullptr);
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "No field at " << index
<< ", max is " << numberOfFields() << ", returning nullopt."
<< std::endl;
return std::nullopt;
}
}
......@@ -146,11 +159,11 @@ public:
template<typename ValueType>
class bit_iterator {
using iterator_category = std::random_access_iterator_tag;
using value_type = ValueType;
using difference_type = std::ptrdiff_t;
using pointer = ValueType*;
using reference = ValueType&;
using iterator_category = std::random_access_iterator_tag;
using value_type = ValueType;
using difference_type = std::ptrdiff_t;
using pointer = ValueType*;
using reference = ValueType&;
public:
bit_iterator(const uint8_t *data, int offset,
......@@ -280,12 +293,12 @@ public:
this->binaryPointer = new uint8_t[structure.getByteSize()];
}
BasePacketTempl(const BasePacketTempl& other) {
this->structure = new BasePacketStructure(other.getPacketStructure());
this->binaryPointer = new uint8_t[other.structure->getByteSize()];
std::memcpy(this->binaryPointer, other.binaryPointer, other.structure->getByteSize());
}
BasePacketTempl(const BasePacketTempl &other) {
this->structure = new BasePacketStructure(other.getPacketStructure());
this->binaryPointer = new uint8_t[other.structure->getByteSize()];
std::memcpy(this->binaryPointer, other.binaryPointer,
other.structure->getByteSize());
}
virtual ~BasePacketTempl() = default;
......@@ -293,9 +306,9 @@ public:
size_t newSize = structure.getByteSize();
size_t oldSize = this->structure->getByteSize();
uint8_t* buff = new uint8_t[newSize];
uint8_t *buff = new uint8_t[newSize];
std::memset(buff, 0, newSize);
std::memcpy(buff, binaryPointer, std::min(newSize,oldSize));
std::memcpy(buff, binaryPointer, std::min(newSize, oldSize));
delete this->binaryPointer;
this->binaryPointer = new uint8_t[newSize];
......@@ -304,8 +317,6 @@ public:
this->structure = &structure;
}
std::optional<ValueType> readValueFromBinaryAt(uint index) const {
auto offset = structure->bitOffsetOf(index); // offset from the beginning of the byte
auto num_bits = structure->bitSizeOf(index); //remaining bits to read
......@@ -313,9 +324,11 @@ public:
return _readValueFromBinaryAt_(binaryPointer, offset.value(),
num_bits.value());
else {
std::cerr << "Error: No field at " << index << ", max is "
<< structure->numberOfFields() << ", returning nullopt"
<< std::endl;
time_t now = time(nullptr) ;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "Error: No field at " << index
<< ", max is " << structure->numberOfFields()
<< ", returning nullopt" << std::endl;
return std::nullopt;
}
}
......@@ -327,11 +340,14 @@ public:
int copyToBinaryPointer(const uint8_t *from, uint size) {
uint max_writable = this->structure->getByteSize();
if (size > max_writable) {
std::cerr << "Error: you are trying to copy " << size
<< " byte where the max size is: " << max_writable
<< std::endl;
std::cerr << "\tI copy only until " << max_writable << " byte"
time_t now = time(nullptr) ;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "Error: you are trying to copy "
<< size << " byte where the max size is: " << max_writable
<< std::endl;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "\tI copy only until "
<< max_writable << " byte" << std::endl;
std::memcpy(binaryPointer, from, max_writable);
return max_writable;
} else {
......@@ -343,17 +359,23 @@ public:
int copyToBinaryPointer(const uint8_t *from, uint size, uint offset) {
uint max_writable = this->structure->getByteSize();
if (offset > max_writable) {
std::cerr << "Error: you are trying to copy starting from "
<< offset << " byte where the max size is: " << max_writable
time_t now = time(nullptr) ;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t"
<< "Error: you are trying to copy starting from " << offset
<< " byte where the max size is: " << max_writable
<< std::endl;
return -1;
}
if (size + offset > max_writable) {
std::cerr << "Error: you are trying to copy " << size + offset
<< " byte where the max size is: " << max_writable
<< std::endl;
std::cerr << "\tI copy only until " << max_writable << " byte"
<< std::endl;
time_t now = time(nullptr) ;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "Error: you are trying to copy "
<< size + offset << " byte where the max size is: "
<< max_writable << std::endl;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "\tI copy only until "
<< max_writable << " byte" << std::endl;
int to_write = max_writable - offset;
std::memcpy(&binaryPointer[offset], from, to_write);
return to_write;
......@@ -397,8 +419,10 @@ public:
if (!offset.has_value() || !numbits.has_value())
return std::nullopt;
if (numbits < min_req) {
std::cerr << "Error: you are trying to write " << value
<< " which requires at least " << min_req
time_t now = time(nullptr) ;
std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
<< "]\t[Base Packet]\t" << "Error: you are trying to write "
<< value << " which requires at least " << min_req
<< " bits in a field of size " << numbits << std::endl;
return std::nullopt;
}
......