Skip to content
Snippets Groups Projects
Astri_Horn_Generic.cpp 2.06 KiB
/*
 * AstriPacketGeneric.cpp
 *
 *  Created on: Jan 26, 2023
 *      Author: valerio
 */

#include <Astri_Horn_Generic.h>
#include <Astri_Horn_Recognized_Packet.h>
#include <ctime>
#include <iomanip>

using namespace inaf::oasbo::Packets;

uint AstriHornGeneric::getHeaderSize() const {
	return 6;
}

uint AstriHornGeneric::getPayloadSize() const {
	auto val = this->operator []("Packet Length");
	if (val.has_value())
		return val.value() + 1;
	else {
		time_t now = time(nullptr);
		std::cerr << "[" << std::put_time(localtime(&now), "%Y-%m-%d %H:%M:%S")
				<< "]\t[AstriHorn Packet]\t"
				<< "Cannot access index 5 to get payload size." << std::endl;
		return -1;
	}
}

uint AstriHornGeneric::getTailSize() const {
	return 0;
}

bool AstriHornGeneric::isRecognizedHeader() const {
	std::optional<size_t> type = this->operator []("Packet Type");
	if (!type.has_value())
		return false;
	std::optional<size_t> subtype = this->operator []("Packet SubType");
	if (!subtype.has_value())
		return false;
	std::optional<size_t> length = this->operator []("Packet Length");
	if (!length.has_value())
		return false;
	size_t totLength = this->getHeaderSize() + length.value() + 1
			+ this->getTailSize();

	std::tuple<size_t, size_t, size_t> target = std::make_tuple(type.value(),
			subtype.value(), totLength);
	auto it = std::find(AstriHorn::recognizedPackets.begin(),
			AstriHorn::recognizedPackets.end(), target);
	if (it != AstriHorn::recognizedPackets.end())
		return true;
	else
		return false;
}

bool AstriHornGeneric::isRecognizedHeader(std::vector<uint8_t> buff) const {
	if (buff.size() != this->getHeaderSize())
		return false;

	size_t type = buff[1] >> 4;
	size_t subtype = buff[1] & 0x0F;
	ssize_t length = (buff[4] << 8) + buff[5];
	ssize_t totLength = this->getHeaderSize() + length + 1
			+ this->getTailSize();
	std::tuple<size_t, size_t, size_t> target = std::make_tuple(type, subtype,
			totLength);
	auto it = std::find(AstriHorn::recognizedPackets.begin(),
			AstriHorn::recognizedPackets.end(), target);
	if (it != AstriHorn::recognizedPackets.end())
		return true;
	else
		return false;
}