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 (9)
......@@ -27,6 +27,7 @@ public:
virtual int open()=0;
virtual int close()=0;
virtual void setDest(std::string dest) = 0;
virtual void updateDest()=0;
virtual std::string getDest() = 0;
virtual ~BaseArchiver() = default;
......
......@@ -11,47 +11,53 @@
namespace inaf::oasbo::DAQ{
class BaseDAQ {
public:
static const int STOP = -1;
static const int READY = 0;
static const int IDLE = 1;
static const int RUN = 2;
enum Status {
STOP = -1,
READY = 0,
IDLE = 1,
RUN = 2
};
protected:
int currentState;
int nextState;
Status currentState;
Status nextState;
bool changeStateFlag = false;
ConnectionProtocols::BaseProtocol *socket;
Archivers::BaseArchiver *archiver;
Providers::BaseProvider *provider;
Monitors::BaseMonitor *monitor;
PacketLib::BasePacket *packet;
public:
void setProtocol(ConnectionProtocols::BaseProtocol &socket){this->socket = &socket;}
void setArchiver(Archivers::BaseArchiver &archiver){this->archiver = &archiver;}
void setProvider(Providers::BaseProvider &provider){this->provider = &provider;}
void setMonitor(Monitors::BaseMonitor &monitor){this->monitor = &monitor;}
void setCurrentState(int currentState){this->currentState = currentState;}
void setNextState(int nextState){this->nextState = nextState;}
void setPacket(PacketLib::BasePacket &packet){this->packet = &packet;}
void setCurrentState(Status currentState){this->currentState = currentState;}
void setNextState(Status nextState){this->nextState = nextState;}
void setChangeStateFlag(bool flag){this->changeStateFlag = flag;}
ConnectionProtocols::BaseProtocol * getSocketPtr(){return this->socket;}
Archivers::BaseArchiver *getArchiverPtr(){return this->archiver;}
Providers::BaseProvider * getProviderPtr(){return this->provider;}
Monitors::BaseMonitor * getMonitorPtr(){return this->monitor;}
int getCurrentState(){return this->currentState;}
int getNextState(){return this->nextState;}
PacketLib::BasePacket * getPacketPrt(){return this->packet;}
Status getCurrentState(){return this->currentState;}
Status getNextState(){return this->nextState;}
int getChangeStateFlag(){return this->changeStateFlag;}
virtual void init() = 0;
virtual void start(int initialState,PacketLib::BasePacket &) = 0;
virtual void start(Status initialState) = 0;
virtual void stop() = 0;
virtual void switchState(const int) = 0;
virtual int deliverPacket(PacketLib::BasePacket &) = 0;
virtual void switchState(const Status) = 0;
virtual int deliverPacket() = 0;
virtual ~BaseDAQ(){
delete socket;
delete provider;
delete archiver;
delete monitor;
delete packet;
}
};
}
......
......@@ -4,9 +4,7 @@
* Created on: Nov 24, 2022
* Author: valerio
*/
#ifndef INCLUDE_BASEPACKET_H_
#define INCLUDE_BASEPACKET_H_
#pragma once
#include <vector>
#include <unordered_map>
......@@ -24,33 +22,33 @@ namespace inaf::oasbo::PacketLib {
class BasePacketStructure {
protected:
std::string source;
size_t byteSize;
std::vector<int> fieldSizes;
std::unordered_map<size_t, size_t> fieldNameToOffsetsMap;
std::unordered_map<std::string, int> fieldNameToIndexMap;
std::unordered_map<int, std::string> indexToFieldNameMap;
uint byteSize;
std::vector<uint> fieldSizes;
std::unordered_map<uint, uint> indexToOffsetsMap;
std::unordered_map<std::string, uint> fieldNameToIndexMap;
std::unordered_map<uint, std::string> indexToFieldNameMap;
virtual std::vector<std::tuple<int, std::string, int>> readStructureFromSource(
virtual std::vector<std::tuple<uint, std::string, uint>> readStructureFromSource(
std::string source) = 0;
void updateFieldSizes(
const std::vector<std::tuple<int, std::string, int>> &paramsTuple) {
const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
std::for_each(paramsTuple.begin(), paramsTuple.end(),
[&](const std::tuple<int, std::string, int> &tup) {
[&](const std::tuple<uint, std::string, uint> &tup) {
this->fieldSizes.push_back(std::get<2>(tup));
});
}
void updateFieldOffsets(
const std::vector<std::tuple<int, std::string, int>> &paramsTuple) {
size_t offset = 0;
const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
uint offset = 0;
for (size_t i = 0; i < paramsTuple.size(); i++) {
fieldNameToOffsetsMap[i] = offset;
indexToOffsetsMap[i] = offset;
offset += std::get<2>(paramsTuple[i]);
}
}
void updateFieldNameAndIndexMap(
const std::vector<std::tuple<int, std::string, int>> &paramsTuple) {
const std::vector<std::tuple<uint, std::string, uint>> &paramsTuple) {
std::for_each(paramsTuple.begin(), paramsTuple.end(),
[&](const std::tuple<int, std::string, int> &tup) {
[&](const std::tuple<uint, std::string, uint> &tup) {
this->fieldNameToIndexMap[std::get<1>(tup)] = std::get<0>(
tup);
this->indexToFieldNameMap[std::get<0>(tup)] = std::get<1>(
......@@ -59,7 +57,7 @@ protected:
}
void updateStructure(std::string source) {
this->source = source;
std::vector<std::tuple<int, std::string, int>> paramsTuple =
std::vector<std::tuple<uint, std::string, uint>> paramsTuple =
readStructureFromSource(source);
updateFieldSizes(paramsTuple);
updateFieldOffsets(paramsTuple);
......@@ -73,22 +71,22 @@ public:
void changeSource(std::string source) {
updateStructure(source);
}
size_t getByteSize() {
uint getByteSize() {
return this->byteSize;
}
std::string getSource() {
return this->source;
}
std::optional<size_t> bitOffsetOf(uint index) {
std::optional<uint> bitOffsetOf(uint index) {
if (index <= this->numberOfFields()) {
return fieldNameToOffsetsMap.at(index);
return indexToOffsetsMap.at(index);
} else {
std::cerr << "No field at " << index << ", max is "
<< numberOfFields() << ", returning nullopt." << std::endl;
return std::nullopt;
}
}
std::optional<size_t> bitSizeOf(uint index) {
std::optional<uint> bitSizeOf(uint index) {
if (index <= this->numberOfFields()) {
return this->fieldSizes[index];
} else {
......@@ -98,7 +96,7 @@ public:
}
}
std::optional<size_t> indexOfField(std::string fieldName) {
std::optional<uint> indexOfField(std::string fieldName) {
try {
return this->fieldNameToIndexMap.at(fieldName);
} catch (const std::out_of_range &oor) {
......@@ -108,7 +106,7 @@ public:
}
}
std::optional<std::string> fieldNameOfIndex(size_t index) {
std::optional<std::string> fieldNameOfIndex(uint index) {
try {
return this->indexToFieldNameMap.at(index);
} catch (const std::out_of_range &oor) {
......@@ -117,11 +115,11 @@ public:
return std::nullopt;
}
}
size_t numberOfFields() {
uint numberOfFields() {
return this->fieldSizes.size();
}
std::unordered_map<std::string, int> getFieldNameToIndexMap() const {
std::unordered_map<std::string, uint> getFieldNameToIndexMap() const {
return this->fieldNameToIndexMap;
}
};
......@@ -132,7 +130,7 @@ class bit_iterator: public std::iterator<std::random_access_iterator_tag,
public:
bit_iterator(const uint8_t *data, int offset,
BasePacketStructure *structure,
std::function<ValueType(const uint8_t*, size_t, int)> func) :
std::function<ValueType(const uint8_t*, uint, uint)> func) :
m_data(data), m_offset(offset), m_structure(structure), m_func(func) {
}
......@@ -193,7 +191,7 @@ private:
const uint8_t *m_data;
int m_offset;
BasePacketStructure *m_structure;
std::function<ValueType(const uint8_t*, size_t, int)> m_func;
std::function<ValueType(const uint8_t*, uint, uint)> m_func;
};
template<typename ValueType>
......@@ -206,17 +204,17 @@ protected:
// The binary value is represented by num_bits number of bits starting from the offset-th bit in the memory.
// The function returns the value of the binary as a ValueType.
static ValueType _readValueFromBinaryAt_(const uint8_t *binaryPointer,
size_t offset, int num_bits) {
uint offset, uint num_bits) {
// Calculate the bit offset from the byte offset:
int bit_offset = offset % 8;
auto bit_offset = offset % 8;
// Calculate the byte offset from the bit offset:
int byte_offset = offset / 8;
auto byte_offset = offset / 8;
ValueType value = 0;
for (int i = 0; i < num_bits; i++) {
for (uint i = 0; i < num_bits; i++) {
// Calculate the byte and bit index of the current bit:
int byte_index = byte_offset + (bit_offset + i) / 8;
int bit_index = (bit_offset + i) % 8;
auto byte_index = byte_offset + (bit_offset + i) / 8;
auto bit_index = (bit_offset + i) % 8;
uint8_t byte = binaryPointer[byte_index];
// Create a bit mask to isolate the desired bit:
uint8_t bit_mask = 1 << (7 - bit_index);
......@@ -226,7 +224,7 @@ protected:
return value;
}
int minBitsRequired(size_t value) {
uint minBitsRequired(size_t value) const {
// Handle special cases
if (value == 0) {
return 1;
......@@ -235,7 +233,7 @@ protected:
}
// Calculate the number of bits needed
int bitsNeeded = 0;
auto bitsNeeded = 0;
bool isNegative = value < 0;
uint32_t absValue = isNegative ? -value : value;
......@@ -260,10 +258,18 @@ public:
virtual ~BasePacketTempl() = default;
void updateStructure(BasePacketStructure &structure) {
size_t newSize = std::min(structure.getByteSize(), this->structure->getByteSize());
this->structure = &structure;
uint8_t *buff = new uint8_t[newSize];
std::memcpy(buff, binaryPointer, newSize);
delete this->binaryPointer;
this->binaryPointer = new uint8_t[newSize];
std::memcpy(binaryPointer, buff, newSize);
delete buff;
}
std::optional<ValueType> readValueFromBinaryAt(uint index) {
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
if (offset.has_value() && num_bits.has_value())
......@@ -281,35 +287,52 @@ public:
return binaryPointer;
}
void copyToBinaryPointer(const uint8_t *from, uint size) {
if (size > this->structure->getByteSize()) {
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: "
<< this->structure->getByteSize() << std::endl;
<< max_writable << std::endl;
std::cerr << "\tI copy only until "
<< this->structure->getByteSize() << " byte" << std::endl;
std::memcpy(binaryPointer, from, this->structure->getByteSize());
} else
<< max_writable << " byte" << std::endl;
std::memcpy(binaryPointer, from, max_writable);
return max_writable;
}
else{
std::memcpy(binaryPointer, from, size);
return size;
}
}
void copyToBinaryPointer(const uint8_t *from, uint size, uint offset) {
if (size + offset > this->structure->getByteSize()) {
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 << 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: "
<< this->structure->getByteSize() << std::endl;
<< max_writable << std::endl;
std::cerr << "\tI copy only until "
<< this->structure->getByteSize() << " byte" << std::endl;
std::memcpy(&binaryPointer[offset], from,
this->structure->getByteSize());
} else
<< max_writable << " byte" << std::endl;
int to_write = max_writable-offset;
std::memcpy(&binaryPointer[offset], from, to_write);
return to_write;
}
else{
std::memcpy(&binaryPointer[offset], from, size);
return size;
}
}
std::optional<ValueType> operator[](uint index) {
std::optional<ValueType> operator[](uint index) const {
return readValueFromBinaryAt(index);
}
std::optional<ValueType> operator[](std::string fieldName) {
std::optional<ValueType> operator[](std::string fieldName) const {
auto index = structure->indexOfField(fieldName);
if (index.has_value())
return readValueFromBinaryAt(index.value());
......@@ -328,7 +351,7 @@ public:
&_readValueFromBinaryAt_);
}
std::optional<int> writeValueToBinaryAtIndex(size_t index,
std::optional<int> writeValueToBinaryAtIndex(uint index,
ValueType value) {
auto offset = this->structure->bitOffsetOf(index);
auto numbits = this->structure->bitSizeOf(index);
......@@ -364,11 +387,15 @@ public:
}
return numbits_written;
}
uint getPacketStructureByteSize() const {return structure->getByteSize();}
BasePacketStructure & getPacketStructure() const { return *(this->structure);}
virtual size_t getHeaderSize()=0;
virtual size_t getPayloadSize()=0;
virtual size_t getTailSize()=0;
virtual size_t getMaxPacketSize()=0;
virtual bool isRecognizedHeader() const = 0;
virtual bool isRecognizedHeader(std::vector<uint8_t> buff) const = 0;
virtual uint getHeaderSize() const = 0;
virtual uint getPayloadSize() const = 0;
virtual uint getTailSize() const = 0;
};
using valueType = size_t;
......@@ -384,4 +411,4 @@ public:
};
}
#endif
......@@ -27,19 +27,19 @@ namespace inaf::oasbo::ConnectionProtocols{
class BaseProtocol{
protected:
std::string host;
int port;
public:
std::string getHost(){return host;}
int getPort(){return port;}
void setHost(std::string host){ this->host = host;}
void setPort(int port){this->port = port;}
virtual int connectToClient()=0;
virtual int connectToServer()=0;
virtual int rcvPacketFromCli(PacketLib::BasePacket &) = 0;
virtual int sendPacketToSrv(PacketLib::BasePacket &) = 0;
virtual int connectToClient()=0;
virtual int closeConnectionToServer()=0;
virtual int closeConnectionToClient()=0;
virtual bool isConnectedToClient() const =0;
virtual bool isConnectedToServer() const =0;
virtual int receiveFromClient(PacketLib::BasePacket &) = 0;
virtual int sendToServer(PacketLib::BasePacket &) = 0;
virtual ~BaseProtocol() = default;
};
}
......