/* * messaging_packet.hpp * * Created on: Mar 27, 2021 * Author: erki */ #ifndef SKULLC_MESSAGING_PACKET_HPP_ #define SKULLC_MESSAGING_PACKET_HPP_ #include #include #include namespace Messaging { template struct Packet { using length_type = std::uint32_t; static constexpr std::uint8_t preamble[] = {'A', 'A'}; std::array data = {0}; std::uint32_t data_length = 0; const std::uint32_t max_data_length = N; const std::uint32_t preamble_length = sizeof(preamble); const std::uint32_t data_length_length = sizeof(data_length); static constexpr std::size_t totalLength() { return N + sizeof(preamble) + sizeof(data_length); } Packet() = default; Packet(const Packet&) = default; Packet(Packet&&) noexcept = default; template void copy_data_in(const std::uint8_t (&data_in)[data_in_length]) { const std::uint32_t to_copy_length = std::min(std::uint32_t(data_in_length), max_data_length); std::memcpy(data.data(), data_in, to_copy_length); data_length = to_copy_length; } template void copy_data_in(const std::array& data_in) { const std::uint32_t to_copy_length = std::min(std::uint32_t(data_in_length), max_data_length); std::memcpy(data.data(), data_in.data(), to_copy_length); data_length = to_copy_length; } bool serialize(std::uint8_t* buffer, const std::uint32_t max_length) { const std::uint32_t required_size = preamble_length + data_length_length + data_length; if (max_length < required_size) return false; std::memcpy(buffer, preamble, preamble_length); buffer += preamble_length; std::memcpy(buffer, &data_length, data_length_length); buffer += data_length_length; std::memcpy(buffer, data.data(), data_length); return true; } bool deserialize(const std::uint8_t* buffer, const std::uint32_t length) { const std::uint32_t header_length = preamble_length + data_length_length; if (length < header_length) return false; if (std::memcmp(buffer, preamble, preamble_length) != 0) return false; std::memcpy(&data_length, buffer + preamble_length, preamble_length); const std::uint32_t final_length = header_length + data_length; if (length != final_length) return false; std::memcpy(data.data(), buffer + header_length, data_length); return true; } }; template constexpr std::uint8_t Packet::preamble[2]; }// namespace Messaging #endif /* SKULLC_MESSAGING_PACKET_HPP_ */