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