Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/tools/socktap/udp_link.cpp
T
Ashin Walpola 0e9525162d Keep the colleague's microbu-esp32c5 tree in this repository
obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but
that tree was gitignored, so a clone of this repository could not build the
firmware it ships. It is now committed here as ordinary files in its own
folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP
bridge's signature verification (--trust) used on the bench. Nothing is
fetched from or pushed to the colleague's repository; this repository and
its remotes carry everything. The folder's own .gitignore keeps build output,
downloaded components and private key material out, as it did there; the
committed file set is identical to that repository's tracked files.

The ESP32-C5 is still flashed from obu-firmware/, which only takes
vanetza-idf from microbu-esp32c5/, so the two stay separate folders.
FLASHING.md says how to take a newer version of the colleague's tree (copy
it over the folder, rebuild, test, commit).
2026-09-23 17:46:40 +02:00

76 lines
2.9 KiB
C++

#include "udp_link.hpp"
#include <vanetza/access/data_request.hpp>
#include <vanetza/net/ethernet_header.hpp>
#include <boost/asio/ip/multicast.hpp>
#include <iostream>
namespace ip = boost::asio::ip;
using namespace vanetza;
UdpLink::UdpLink(boost::asio::io_context& io_context, const ip::udp::endpoint& endpoint, const EthernetDevice& device) :
multicast_endpoint_(endpoint),
tx_socket_(io_context), rx_socket_(io_context),
rx_buffer_(2560, 0x00)
{
auto ip = device.ip();
tx_socket_.open(multicast_endpoint_.protocol());
if (!ip.is_unspecified()) {
boost::asio::ip::multicast::outbound_interface option(ip);
tx_socket_.set_option(option);
}
rx_socket_.open(multicast_endpoint_.protocol());
rx_socket_.set_option(ip::udp::socket::reuse_address(true));
rx_socket_.bind(multicast_endpoint_);
rx_socket_.set_option(ip::multicast::enable_loopback(false));
if(!ip.is_unspecified() && multicast_endpoint_.address().is_v4()) {
rx_socket_.set_option(ip::multicast::join_group(multicast_endpoint_.address().to_v4(), ip));
} else {
rx_socket_.set_option(ip::multicast::join_group(multicast_endpoint_.address()));
}
do_receive();
}
void UdpLink::indicate(IndicationCallback cb)
{
callback_ = cb;
}
void UdpLink::do_receive()
{
rx_socket_.async_receive_from(boost::asio::buffer(rx_buffer_), rx_endpoint_,
[this](boost::system::error_code ec, std::size_t length) {
if (!ec) {
ByteBuffer buffer(rx_buffer_.begin(), rx_buffer_.begin() + length);
CohesivePacket packet(std::move(buffer), OsiLayer::Link);
if (packet.size(OsiLayer::Link) < EthernetHeader::length_bytes) {
std::cerr << "Dropped UDP packet too short to contain Ethernet header\n";
} else {
packet.set_boundary(OsiLayer::Link, EthernetHeader::length_bytes);
auto link_range = packet[OsiLayer::Link];
EthernetHeader eth = decode_ethernet_header(link_range.begin(), link_range.end());
if (callback_) {
callback_(std::move(packet), eth);
}
}
do_receive();
}
});
}
void UdpLink::request(const access::DataRequest& request, std::unique_ptr<ChunkPacket> packet)
{
packet->layer(OsiLayer::Link) = create_ethernet_header(request.destination_addr, request.source_addr, request.ether_type);
std::array<boost::asio::const_buffer, layers_> const_buffers;
for (auto& layer : osi_layer_range<OsiLayer::Link, OsiLayer::Application>()) {
const auto index = distance(OsiLayer::Link, layer);
packet->layer(layer).convert(tx_buffers_[index]);
const_buffers[index] = boost::asio::buffer(tx_buffers_[index]);
}
tx_socket_.send_to(const_buffers, multicast_endpoint_);
}