Files
MicrOBU/obu-firmware/external/vanetza-idf/vanetza/net/ethernet_header.cpp
T
Ashin Walpola d107534eb2 Keep vanetza-idf in obu-firmware, so a plain clone builds the firmware
obu-firmware builds against the vanetza-idf C-ITS library, which until now
came from the colleague's microbu-esp32c5 tree beside the repository and was
not tracked here, so a clone of this repository could not build the firmware
it ships. The library alone is now part of obu-firmware, as
obu-firmware/external/vanetza-idf: their external/vanetza-idf at commit
cf4b99f, unchanged (9775 files; see its PROVENANCE.md). CMake takes it from
there by default; -DVANETZA_IDF_DIR still points the build elsewhere.

The rest of the colleague's tree (their own VAM firmware, PKI tooling,
station-link Python tools, the V2X2MAP bridge) stays out of this repository
and gitignored; nothing is pushed to their repository. NOTES.md, docs/06,
TODO.md and the pcap verifier's usage line point at the new location.
2026-09-24 10:56:05 +02:00

52 lines
1.6 KiB
C++

#include "ethernet_header.hpp"
#include <algorithm>
#include <cassert>
#include <stdexcept>
namespace vanetza
{
ByteBuffer create_ethernet_header(const MacAddress& dest, const MacAddress& src, uint16be_t proto)
{
ByteBuffer buffer;
buffer.reserve(EthernetHeader::length_bytes);
auto inserter = std::back_inserter(buffer);
std::copy(dest.octets.begin(), dest.octets.end(), inserter);
std::copy(src.octets.begin(), src.octets.end(), inserter);
uint16_t host_proto = proto.host();
inserter = (host_proto >> 8) & 0xff;
inserter = host_proto & 0xff;
assert(buffer.size() == EthernetHeader::length_bytes);
return buffer;
}
ByteBuffer create_ethernet_header(const EthernetHeader& hdr)
{
return create_ethernet_header(hdr.destination, hdr.source, hdr.type);
}
EthernetHeader decode_ethernet_header(ByteBuffer::const_iterator begin, ByteBuffer::const_iterator end)
{
EthernetHeader hdr;
const std::size_t buflen = std::distance(begin, end);
if (buflen < EthernetHeader::length_bytes) {
throw std::runtime_error("buffer is too short for EthernetHeader decoding");
} else {
std::copy_n(begin, MacAddress::length_bytes, hdr.destination.octets.begin());
begin += MacAddress::length_bytes;
std::copy_n(begin, MacAddress::length_bytes, hdr.source.octets.begin());
begin += MacAddress::length_bytes;
uint16_t proto = (begin[0] << 8) | begin[1];
hdr.type = host_cast(proto);
}
return hdr;
}
EthernetHeader decode_ethernet_header(const ByteBuffer& buf)
{
return decode_ethernet_header(buf.begin(), buf.end());
}
} // namespace vanetza