Files
MicrOBU/obu-firmware/external/vanetza-idf/vanetza/geonet/packet_buffer.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

95 lines
2.1 KiB
C++

#include "packet_buffer.hpp"
#include "basic_header.hpp"
namespace vanetza
{
namespace geonet
{
namespace packet_buffer
{
Expiry::Expiry(Clock::time_point now, Clock::duration lifetime) :
m_buffered_since(now), m_expires_at(now)
{
m_expires_at += lifetime;
}
bool Expiry::is_expired(Clock::time_point now) const
{
return (m_expires_at < now);
}
} // namespace packet_buffer
PacketBuffer::PacketBuffer(std::size_t capacity) :
m_capacity(capacity), m_stored(0)
{
}
bool PacketBuffer::push(data_ptr data, Clock::time_point now)
{
assert(data);
bool pushed = false;
drop_expired(now);
if (drop(data->length())) {
m_stored += data->length();
m_nodes.push_back(std::make_tuple(
expiry_type(now, data->reduce_lifetime(Clock::duration::zero())),
std::move(data)
));
pushed = true;
}
return pushed;
}
void PacketBuffer::flush(Clock::time_point now)
{
decltype(m_nodes) nodes;
std::swap(m_nodes, nodes);
m_stored = 0;
for (auto& node : nodes) {
const auto& expiry = std::get<0>(node);
if (!expiry.is_expired(now)) {
auto& data = std::get<1>(node);
const auto queuing_time = now - expiry.buffered_since();
data->reduce_lifetime(queuing_time);
data->flush();
}
}
}
bool PacketBuffer::drop_head()
{
bool dropped = !m_nodes.empty();
if (dropped) {
m_stored -= std::get<1>(m_nodes.front())->length();
m_nodes.pop_front();
}
return dropped;
}
void PacketBuffer::drop_expired(Clock::time_point now)
{
std::list<decltype(m_nodes)::iterator> expired;
for (auto it = m_nodes.begin(), end = m_nodes.end(); it != end;) {
if (std::get<0>(*it).is_expired(now)) {
m_stored -= std::get<1>(*it)->length();
it = m_nodes.erase(it);
} else {
++it;
}
}
}
bool PacketBuffer::drop(std::size_t bytes)
{
while (free() < bytes && drop_head()) {}
return free() >= bytes;
}
} // namespace geonet
} // namespace vanetza