Skip to content
Base_Packet.h 14.4 KiB
Newer Older
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
#pragma once
Valerio Pastore's avatar
Valerio Pastore committed

#include <vector>
Valerio Pastore's avatar
Valerio Pastore committed
#include <unordered_map>
Valerio Pastore's avatar
Valerio Pastore committed
#include <string>
#include <cstring>
#include <algorithm>
Valerio Pastore's avatar
Valerio Pastore committed
#include <numeric>
Valerio Pastore's avatar
Valerio Pastore committed
#include <iostream>
#include <cmath>
Valerio Pastore's avatar
Valerio Pastore committed
#include <functional>
Valerio Pastore's avatar
Valerio Pastore committed
#include <optional>
Valerio Pastore's avatar
Valerio Pastore committed
#include <ctime>
#include <iomanip>
Valerio Pastore's avatar
Valerio Pastore committed
namespace inaf::oasbo::PacketLib {

Valerio Pastore's avatar
Valerio Pastore committed
class BasePacketStructure {
protected:
	using Structure = std::vector<std::tuple<uint, std::string, uint>>;

Valerio Pastore's avatar
Valerio Pastore committed
	std::string source;
	Structure structure;
Valerio Pastore's avatar
Valerio Pastore committed
	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;
	std::function<Structure(std::string)> sourceReadingFunc;
Valerio Pastore's avatar
Valerio Pastore committed
	void updateFieldSizes(const Structure &paramsTuple) {
Valerio Pastore's avatar
Valerio Pastore committed
		std::for_each(paramsTuple.begin(), paramsTuple.end(),
Valerio Pastore's avatar
Valerio Pastore committed
				[&](const std::tuple<uint, std::string, uint> &tup) {
Valerio Pastore's avatar
Valerio Pastore committed
					this->fieldSizes.push_back(std::get<2>(tup));
				});
	}
Valerio Pastore's avatar
Valerio Pastore committed
	void updateFieldOffsets(const Structure &paramsTuple) {
Valerio Pastore's avatar
Valerio Pastore committed
		uint offset = 0;
Valerio Pastore's avatar
Valerio Pastore committed
		for (size_t i = 0; i < paramsTuple.size(); i++) {
Valerio Pastore's avatar
Valerio Pastore committed
			indexToOffsetsMap[i] = offset;
Valerio Pastore's avatar
Valerio Pastore committed
			offset += std::get<2>(paramsTuple[i]);
		}
	}
Valerio Pastore's avatar
Valerio Pastore committed
	void updateFieldNameAndIndexMap(const Structure &paramsTuple) {
Valerio Pastore's avatar
Valerio Pastore committed
		std::for_each(paramsTuple.begin(), paramsTuple.end(),
Valerio Pastore's avatar
Valerio Pastore committed
				[&](const std::tuple<uint, std::string, uint> &tup) {
Valerio Pastore's avatar
Valerio Pastore committed
					this->fieldNameToIndexMap[std::get<1>(tup)] = std::get<0>(
							tup);
					this->indexToFieldNameMap[std::get<0>(tup)] = std::get<1>(
							tup);
				});
	}
Valerio Pastore's avatar
Valerio Pastore committed
	void updateStructure(std::string source) {
Valerio Pastore's avatar
Valerio Pastore committed
		this->source = source;
		this->structure = sourceReadingFunc(source);
Valerio Pastore's avatar
Valerio Pastore committed
		updateFieldSizes(structure);
		updateFieldOffsets(structure);
		updateFieldNameAndIndexMap(structure);
Valerio Pastore's avatar
Valerio Pastore committed
		uint bitSize = std::accumulate(fieldSizes.begin(), fieldSizes.end(), 0);
		this->byteSize = bitSize % 8 == 0 ? bitSize / 8 : bitSize / 8 + 1;
Valerio Pastore's avatar
Valerio Pastore committed
	}

public:
	virtual ~BasePacketStructure() = default;
Valerio Pastore's avatar
Valerio Pastore committed
	BasePacketStructure(std::string source,
			std::function<Structure(std::string)> sourceReadingFunc) :
			source(source), sourceReadingFunc(sourceReadingFunc) {
		this->updateStructure(source);
Valerio Pastore's avatar
Valerio Pastore committed
	}
	;
	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;
Valerio Pastore's avatar
Valerio Pastore committed
	void changeSource(std::string source) {
Valerio Pastore's avatar
Valerio Pastore committed
		updateStructure(source);
	}
Valerio Pastore's avatar
Valerio Pastore committed
	uint getByteSize() {
Valerio Pastore's avatar
Valerio Pastore committed
		return this->byteSize;
	}
Valerio Pastore's avatar
Valerio Pastore committed
	std::string getSource() {
Valerio Pastore's avatar
Valerio Pastore committed
		return this->source;
	}
Valerio Pastore's avatar
Valerio Pastore committed
	std::optional<uint> bitOffsetOf(uint index) {
Valerio Pastore's avatar
Valerio Pastore committed
		if (index <= this->numberOfFields()) {
Valerio Pastore's avatar
Valerio Pastore committed
			return indexToOffsetsMap.at(index);
Valerio Pastore's avatar
Valerio Pastore committed
		} else {
Valerio Pastore's avatar
Valerio Pastore committed
		    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;
Valerio Pastore's avatar
Valerio Pastore committed
			return std::nullopt;
		}
	}
Valerio Pastore's avatar
Valerio Pastore committed
	std::optional<uint> bitSizeOf(uint index) {
Valerio Pastore's avatar
Valerio Pastore committed
		if (index <= this->numberOfFields()) {
			return this->fieldSizes[index];
		} else {
Valerio Pastore's avatar
Valerio Pastore committed
		    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;
Valerio Pastore's avatar
Valerio Pastore committed
			return std::nullopt;
		}
	}
Valerio Pastore's avatar
Valerio Pastore committed
	std::optional<uint> indexOfField(std::string fieldName) {
Valerio Pastore's avatar
Valerio Pastore committed
		std::transform(fieldName.begin(), fieldName.end(), fieldName.begin(),
				[](unsigned char c) {
					return std::tolower(c);
				});
Valerio Pastore's avatar
Valerio Pastore committed
		try {
			return this->fieldNameToIndexMap.at(fieldName);
		} catch (const std::out_of_range &oor) {
Valerio Pastore's avatar
Valerio Pastore committed
		    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
Valerio Pastore's avatar
Valerio Pastore committed
					<< ", returning nullopt." << std::endl;
			return std::nullopt;
		}
	}

Valerio Pastore's avatar
Valerio Pastore committed
	std::optional<std::string> fieldNameOfIndex(uint index) {
Valerio Pastore's avatar
Valerio Pastore committed
		try {
			return this->indexToFieldNameMap.at(index);
		} catch (const std::out_of_range &oor) {
Valerio Pastore's avatar
Valerio Pastore committed
		    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;
Valerio Pastore's avatar
Valerio Pastore committed
			return std::nullopt;
		}
	}
Valerio Pastore's avatar
Valerio Pastore committed
	uint numberOfFields() {
Valerio Pastore's avatar
Valerio Pastore committed
		return this->fieldSizes.size();
	}

Valerio Pastore's avatar
Valerio Pastore committed
	std::unordered_map<std::string, uint> getFieldNameToIndexMap() const {
Valerio Pastore's avatar
Valerio Pastore committed
		return this->fieldNameToIndexMap;
	}
Valerio Pastore's avatar
Valerio Pastore committed

	std::unordered_map<uint, uint> getIndexToOffsetsMap() const{
		return this->indexToOffsetsMap;
	}
	std::unordered_map<uint, std::string> getIndexToFieldNameMap() const {
		return this->indexToFieldNameMap;
	}

Valerio Pastore's avatar
Valerio Pastore committed
template<typename ValueType>
Valerio Pastore's avatar
Valerio Pastore committed
class bit_iterator {

Valerio Pastore's avatar
Valerio Pastore committed
	using iterator_category = std::random_access_iterator_tag;
	using value_type = ValueType;
	using difference_type = std::ptrdiff_t;
	using pointer = ValueType*;
	using reference = ValueType&;
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
public:
Valerio Pastore's avatar
Valerio Pastore committed
	bit_iterator(const uint8_t *data, int offset,
			BasePacketStructure *structure,
Valerio Pastore's avatar
Valerio Pastore committed
			std::function<ValueType(const uint8_t*, uint, uint)> func) :
Valerio Pastore's avatar
Valerio Pastore committed
			m_data(data), m_offset(offset), m_structure(structure), m_func(func) {
Valerio Pastore's avatar
Valerio Pastore committed
	}

	bit_iterator(const bit_iterator &other) :
Valerio Pastore's avatar
Valerio Pastore committed
			m_data(other.m_data), m_offset(other.m_offset), m_structure(
					other.m_structure), m_func(other.m_func) {
Valerio Pastore's avatar
Valerio Pastore committed
	}

	bit_iterator& operator++() {
		++m_offset;
		return *this;
	}

	bit_iterator operator++(int) {
		bit_iterator temp(*this);
		++m_offset;
		return temp;
	}

	bit_iterator& operator--() {
		--m_offset;
		return *this;
	}

	bit_iterator operator--(int) {
		bit_iterator temp(*this);
		--m_offset;
		return temp;
	}

	bit_iterator operator+(int n) const {
Valerio Pastore's avatar
Valerio Pastore committed
		return bit_iterator(m_data, m_offset + n, m_structure, m_func);
Valerio Pastore's avatar
Valerio Pastore committed
	}

	bit_iterator operator-(int n) const {
Valerio Pastore's avatar
Valerio Pastore committed
		return bit_iterator(m_data, m_offset - n, m_structure, m_func);
Valerio Pastore's avatar
Valerio Pastore committed
	}

	int operator-(const bit_iterator &other) const {
		return m_offset - other.m_offset;
	}

	bool operator==(const bit_iterator &other) const {
		return m_data == other.m_data && m_offset == other.m_offset;
	}

	bool operator!=(const bit_iterator &other) const {
		return !(*this == other);
	}

	ValueType operator*() const {
Valerio Pastore's avatar
Valerio Pastore committed
		auto offset = m_structure->bitOffsetOf(m_offset); // offset from the beginning of the byte
		auto num_bits = m_structure->bitSizeOf(m_offset);
		return m_func(m_data, offset.value(), num_bits.value());
Valerio Pastore's avatar
Valerio Pastore committed
	}

private:
	const uint8_t *m_data;
	int m_offset;
Valerio Pastore's avatar
Valerio Pastore committed
	BasePacketStructure *m_structure;
Valerio Pastore's avatar
Valerio Pastore committed
	std::function<ValueType(const uint8_t*, uint, uint)> m_func;
Valerio Pastore's avatar
Valerio Pastore committed
};

template<typename ValueType>
class BasePacketTempl {
Valerio Pastore's avatar
Valerio Pastore committed
protected:
Valerio Pastore's avatar
Valerio Pastore committed
	BasePacketStructure *packetStructure;
	uint8_t *memoryPointer;
Valerio Pastore's avatar
Valerio Pastore committed
	// This C++ function reads a binary value from a memory location pointed to by binaryPointer.
	// 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.
Valerio Pastore's avatar
Valerio Pastore committed
	static ValueType _readValueFromMemoryAt_(const uint8_t *binaryPointer,
Valerio Pastore's avatar
Valerio Pastore committed
			uint offset, uint num_bits) {
Valerio Pastore's avatar
Valerio Pastore committed
		// Calculate the bit offset from the byte offset:
		uint bit_offset = offset % 8;
Valerio Pastore's avatar
Valerio Pastore committed
		// Calculate the byte offset from the bit offset:
		uint byte_offset = offset / 8;
Valerio Pastore's avatar
Valerio Pastore committed
		ValueType value = 0;
		ValueType bit = 1;
Valerio Pastore's avatar
Valerio Pastore committed
		for (uint i = 0; i < num_bits; i++) {
Valerio Pastore's avatar
Valerio Pastore committed
			// Calculate the byte and bit index of the current bit:
			uint byte_index = byte_offset + (bit_offset + i) / 8;
			uint bit_index = (bit_offset + i) % 8;
Valerio Pastore's avatar
Valerio Pastore committed
			uint8_t byte = binaryPointer[byte_index];
Valerio Pastore's avatar
Valerio Pastore committed
			// Create a bit mask to isolate the desired bit:
Valerio Pastore's avatar
Valerio Pastore committed
			uint8_t bit_mask = 1 << (7 - bit_index);
Valerio Pastore's avatar
Valerio Pastore committed
			// Set the corresponding bit in the return value if the retrieved bit is 1:
			value |= (byte & bit_mask) ? ( bit << (num_bits - i - 1)) : 0;
Valerio Pastore's avatar
Valerio Pastore committed
		}
		return value;
	}
	size_t minBitsRequired(size_t value) const {
Valerio Pastore's avatar
Valerio Pastore committed
		// Handle special cases
		size_t bitsNeeded = 1;
		while (value != 0) {
Valerio Pastore's avatar
Valerio Pastore committed
			bitsNeeded++;
			value >>= 1;
Valerio Pastore's avatar
Valerio Pastore committed
		}

		return bitsNeeded;
	}
Valerio Pastore's avatar
Valerio Pastore committed
public:
Valerio Pastore's avatar
Valerio Pastore committed
	BasePacketTempl(BasePacketStructure &structure) {
Valerio Pastore's avatar
Valerio Pastore committed
		this->packetStructure = new BasePacketStructure(structure);
		this->memoryPointer = new uint8_t[structure.getByteSize()];
Valerio Pastore's avatar
Valerio Pastore committed
	BasePacketTempl(const BasePacketTempl &other) {
Valerio Pastore's avatar
Valerio Pastore committed
		this->packetStructure = new BasePacketStructure(other.getPacketStructure());
		this->memoryPointer = new uint8_t[other.packetStructure->getByteSize()];
		std::memcpy(this->memoryPointer, other.memoryPointer,
				other.packetStructure->getByteSize());
Valerio Pastore's avatar
Valerio Pastore committed
	}
Valerio Pastore's avatar
Valerio Pastore committed
	virtual ~BasePacketTempl() = default;

Valerio Pastore's avatar
Valerio Pastore committed
	void updatePacketStructure(BasePacketStructure &structure) {
Valerio Pastore's avatar
Valerio Pastore committed
		size_t newSize = structure.getByteSize();
Valerio Pastore's avatar
Valerio Pastore committed
		size_t oldSize = this->packetStructure->getByteSize();
Valerio Pastore's avatar
Valerio Pastore committed
		uint8_t *buff = new uint8_t[newSize];
Valerio Pastore's avatar
Valerio Pastore committed
		std::memset(buff, 0, newSize);
Valerio Pastore's avatar
Valerio Pastore committed
		std::memcpy(buff, memoryPointer, std::min(newSize, oldSize));
		delete this->memoryPointer;
Valerio Pastore's avatar
Valerio Pastore committed
		this->memoryPointer = new uint8_t[newSize];
		std::memcpy(memoryPointer, buff, newSize);
Valerio Pastore's avatar
Valerio Pastore committed
		delete[] buff;
Valerio Pastore's avatar
Valerio Pastore committed
		this->packetStructure = &structure;
Valerio Pastore's avatar
Valerio Pastore committed
	}
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
	std::optional<ValueType> readValueFromMemoryAt(uint index) const {
		auto offset = packetStructure->bitOffsetOf(index); // offset from the beginning of the byte
		auto num_bits = packetStructure->bitSizeOf(index); //remaining bits to read
Valerio Pastore's avatar
Valerio Pastore committed
		if (offset.has_value() && num_bits.has_value())
Valerio Pastore's avatar
Valerio Pastore committed
			return _readValueFromMemoryAt_(memoryPointer, offset.value(),
Valerio Pastore's avatar
Valerio Pastore committed
					num_bits.value());
		else {
Valerio Pastore's avatar
Valerio Pastore committed
		    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
Valerio Pastore's avatar
Valerio Pastore committed
					<< ", max is " << packetStructure->numberOfFields()
Valerio Pastore's avatar
Valerio Pastore committed
					<< ", returning nullopt" << std::endl;
Valerio Pastore's avatar
Valerio Pastore committed
			return std::nullopt;
		}
Valerio Pastore's avatar
Valerio Pastore committed
	uint8_t const* getPointerToMemory() const {
		return memoryPointer;
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
	int copyToMemory(const uint8_t *from, uint size) {
		uint max_writable = this->packetStructure->getByteSize();
Valerio Pastore's avatar
Valerio Pastore committed
		if (size > max_writable) {
Valerio Pastore's avatar
Valerio Pastore committed
		    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
Valerio Pastore's avatar
Valerio Pastore committed
					<< std::endl;
Valerio Pastore's avatar
Valerio Pastore committed
			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;
Valerio Pastore's avatar
Valerio Pastore committed
			std::memcpy(memoryPointer, from, max_writable);
Valerio Pastore's avatar
Valerio Pastore committed
			return max_writable;
Valerio Pastore's avatar
Valerio Pastore committed
		} else {
Valerio Pastore's avatar
Valerio Pastore committed
			std::memcpy(memoryPointer, from, size);
Valerio Pastore's avatar
Valerio Pastore committed
			return size;
		}
Valerio Pastore's avatar
Valerio Pastore committed
	int copyToMemory(const uint8_t *from, uint size, uint offset) {
		uint max_writable = this->packetStructure->getByteSize();
Valerio Pastore's avatar
Valerio Pastore committed
		if (offset > max_writable) {
Valerio Pastore's avatar
Valerio Pastore committed
		    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
Valerio Pastore's avatar
Valerio Pastore committed
					<< std::endl;
Valerio Pastore's avatar
Valerio Pastore committed
			return -1;
		}
Valerio Pastore's avatar
Valerio Pastore committed
		if (size + offset > max_writable) {
Valerio Pastore's avatar
Valerio Pastore committed
		    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;
Valerio Pastore's avatar
Valerio Pastore committed
			int to_write = max_writable - offset;
Valerio Pastore's avatar
Valerio Pastore committed
			std::memcpy(&memoryPointer[offset], from, to_write);
Valerio Pastore's avatar
Valerio Pastore committed
			return to_write;
Valerio Pastore's avatar
Valerio Pastore committed
		} else {
Valerio Pastore's avatar
Valerio Pastore committed
			std::memcpy(&memoryPointer[offset], from, size);
Valerio Pastore's avatar
Valerio Pastore committed
			return size;
		}
Valerio Pastore's avatar
Valerio Pastore committed
	std::optional<ValueType> operator[](uint index) const {
Valerio Pastore's avatar
Valerio Pastore committed
		return readValueFromMemoryAt(index);
Valerio Pastore's avatar
Valerio Pastore committed
	std::optional<ValueType> operator[](std::string fieldName) const {
Valerio Pastore's avatar
Valerio Pastore committed
		std::transform(fieldName.begin(), fieldName.end(), fieldName.begin(),
				[](unsigned char c) {
					return std::tolower(c);
				});
Valerio Pastore's avatar
Valerio Pastore committed
		auto index = packetStructure->indexOfField(fieldName);
Valerio Pastore's avatar
Valerio Pastore committed
		if (index.has_value())
Valerio Pastore's avatar
Valerio Pastore committed
			return readValueFromMemoryAt(index.value());
Valerio Pastore's avatar
Valerio Pastore committed
		else
Valerio Pastore's avatar
Valerio Pastore committed
			return std::nullopt;
Valerio Pastore's avatar
Valerio Pastore committed
	bit_iterator<ValueType> begin() const {
Valerio Pastore's avatar
Valerio Pastore committed
		return bit_iterator<ValueType>(memoryPointer, 0, packetStructure,
				&_readValueFromMemoryAt_);
Valerio Pastore's avatar
Valerio Pastore committed
	}

	bit_iterator<ValueType> end() const {
Valerio Pastore's avatar
Valerio Pastore committed
		return bit_iterator<ValueType>(memoryPointer,
				packetStructure->numberOfFields(), packetStructure,
				&_readValueFromMemoryAt_);
Valerio Pastore's avatar
Valerio Pastore committed
	std::optional<uint> writeValueIntoMemoryAtIndex(uint index, ValueType value) {
		auto offset = this->packetStructure->bitOffsetOf(index);
		auto numbits = this->packetStructure->bitSizeOf(index);
Valerio Pastore's avatar
Valerio Pastore committed
		size_t min_req = minBitsRequired(value);
		if (!offset.has_value() || !numbits.has_value())
			return std::nullopt;
		if (numbits < min_req) {
Valerio Pastore's avatar
Valerio Pastore committed
		    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
Valerio Pastore's avatar
Valerio Pastore committed
					<< " bits in a field of size " << numbits << std::endl;
			return std::nullopt;
		}

		// Calculate the bit offset from the byte offset:
		uint bitoffset = offset.value() % 8;
Valerio Pastore's avatar
Valerio Pastore committed
		// Calculate the byte offset from the bit offset:
		uint byteoffset = offset.value() / 8;
		uint numbits_written = 0;
Valerio Pastore's avatar
Valerio Pastore committed
		for (size_t i = 0; i < numbits; i++) {
			// Calculate the byte and bit index of the current bit:
			uint byte_index = byteoffset + (bitoffset + i) / 8;
			uint bit_index = (bitoffset + i) % 8;
Valerio Pastore's avatar
Valerio Pastore committed
			// Create a bit mask to isolate the desired bit:
			uint8_t bit_mask = 1 << (7 - bit_index);
			// Set the corresponding bit in the binary array if the value is 1:
			if ((value >> (numbits.value() - i - 1)) & 1) {
Valerio Pastore's avatar
Valerio Pastore committed
				memoryPointer[byte_index] |= bit_mask;
Valerio Pastore's avatar
Valerio Pastore committed
				numbits_written++;
			} else {
Valerio Pastore's avatar
Valerio Pastore committed
				memoryPointer[byte_index] &= ~bit_mask;
Valerio Pastore's avatar
Valerio Pastore committed
		return numbits_written;
Valerio Pastore's avatar
Valerio Pastore committed

	uint getPacketStructureByteSize() const {
Valerio Pastore's avatar
Valerio Pastore committed
		return packetStructure->getByteSize();
Valerio Pastore's avatar
Valerio Pastore committed
	}
	BasePacketStructure& getPacketStructure() const {
Valerio Pastore's avatar
Valerio Pastore committed
		return *(this->packetStructure);
Valerio Pastore's avatar
Valerio Pastore committed

Valerio Pastore's avatar
Valerio Pastore committed
	virtual bool hasRecognizedHeader() const = 0;
	virtual bool isRecognizedHeader(std::vector<uint8_t> buff) const = 0;
Valerio Pastore's avatar
Valerio Pastore committed
	virtual uint getHeaderSize() const = 0;
	virtual uint getPayloadSize() const = 0;
	virtual uint getTailSize() const = 0;
Valerio Pastore's avatar
Valerio Pastore committed

using valueType = size_t;
Valerio Pastore's avatar
Valerio Pastore committed
class BasePacket: public BasePacketTempl<valueType> {
Valerio Pastore's avatar
Valerio Pastore committed

protected:

public:
Valerio Pastore's avatar
Valerio Pastore committed
	BasePacket(BasePacketStructure &structure) :
Valerio Pastore's avatar
Valerio Pastore committed
			BasePacketTempl<valueType>(structure) {
Valerio Pastore's avatar
Valerio Pastore committed
	}
	virtual ~BasePacket() = default;

};
Valerio Pastore's avatar
Valerio Pastore committed