Rebuild obu-firmware on Niklas Dathe's microbu-firmware

Replace the vanetza-idf port with his current station firmware (microbu-firmware
e4e4bd8: main/, components/microbu_v2x, components/vanetza, external/vanetza) and
re-apply the MicrOBU additions, each marked "MicrOBU:" in the sources:

- V2X_RX (opcode 0x90): every received ITS message, signed or not, through
  gn_unwrap.c, because the stack drops unsigned and foreign-root traffic. 0x85 is
  the spec's RAW_FRAME and 0x86 is reserved. The body is built in v2x_rx.hpp.
- Unsigned transmission (geonet.c) for security profile 1 or station security 0,
  from a random MAC per configuration.
- Radio polled from STATION_CONFIGURE on; manual country policy, TX power
  read-back, static RX buffer, short frames ignored.
- Battery guard (battery_policy.hpp): deep sleep below 3.30 V with no phone on USB.
- No USB writes without a host; last_frame_ms() for the guard.
- 80 KB NVS with the app at 0x20000, 8 MB flash, XIAO ESP32-C5 board, 20 dBm.

Remove the old C firmware, the bench listener and the old vanetza-idf copy. Add
docs/station-link.tex as revision 2 of the interface reference (V2X_RX, unsigned
sending, battery rule, revision history). Add 39 host unit tests (make firmware in
test/host) next to the existing chain, replay, fuzz and battery tests, and point
tools/station_status_watch.py at the new CONFIRM-based status.
This commit is contained in:
Ashin Walpola
2026-11-11 00:13:57 +01:00
parent 1c3a66e4da
commit 50bbf6552d
10307 changed files with 53622 additions and 850451 deletions
+176 -44
View File
@@ -13,31 +13,59 @@ LinkService::LinkService(Station& station) : station_(station) {
station_.on_indication([this](const link::BtpDataIndication& indication) {
broadcast(link::Opcode::BTP_DATA_INDICATION, ++own_sequence_, link::encode(indication));
});
station_.on_raw_frame([this](const link::Bytes& body) {
if (raw_transport_ == LinkTransport::ble && !ble::connected()) return;
send_fragmented(raw_transport_, link::Opcode::RAW_FRAME, ++own_sequence_, body);
});
// MicrOBU: every ITS message heard on air, verified or not (Station::on_raw_its).
station_.on_raw_its([this](const link::Bytes& body) {
broadcast(link::Opcode::V2X_RX, ++own_sequence_, body);
});
// T_GenVam is included in the status snapshot returned on request.
station_.on_id_event([this](const link::IdChangeEvent& event) {
// PREPARE/COMMIT expect SF_IDCHANGE_EVENT_RESPONSE with this sequence
// PREPARE/COMMIT responses match the subscription; their header sequence is unused.
broadcast(link::Opcode::SF_IDCHANGE_EVENT, ++own_sequence_, link::encode(event));
});
}
void LinkService::send(LinkTransport transport, link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body) {
bool LinkService::send(LinkTransport transport, link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body,
std::uint8_t flags) {
link::Bytes octets;
link::Message message {{opcode, 0, sequence}, body};
if (!link::encode(message, octets)) { ESP_LOGE(TAG, "message 0x%02x too long (%u)", unsigned(opcode), unsigned(body.size())); return; }
if (transport == LinkTransport::serial) serial::write(serial::FrameType::LINK, octets);
else ble::write(octets);
link::Message message {{opcode, flags, sequence}, body};
if (!link::encode(message, octets)) { ESP_LOGE(TAG, "message 0x%02x too long (%u)", unsigned(opcode), unsigned(body.size())); return false; }
if (transport == LinkTransport::serial) {
if (opcode == link::Opcode::RAW_FRAME) return serial::try_write(serial::FrameType::LINK, octets);
return serial::write(serial::FrameType::LINK, octets);
}
return ble::write(octets);
}
bool LinkService::send_fragmented(LinkTransport transport, link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body) {
const std::size_t limit = transport == LinkTransport::ble ? ble::max_message() : link::maximum_message;
const std::size_t chunk = limit - link::header_size;
if (body.size() <= chunk) return send(transport, opcode, sequence, body);
if (body.size() > 65535 || chunk <= link::fragment_header_size) return false;
const std::size_t capacity = chunk - link::fragment_header_size;
for (std::size_t offset = 0; offset < body.size(); offset += capacity) {
const std::size_t n = std::min(capacity, body.size() - offset);
const std::uint8_t flags = offset == 0 ? link::flag_fragment_first :
(offset + n == body.size() ? link::flag_fragment_last : link::flag_fragment_middle);
link::Bytes part {std::uint8_t(body.size()), std::uint8_t(body.size() >> 8),
std::uint8_t(offset), std::uint8_t(offset >> 8)};
part.insert(part.end(), body.begin() + offset, body.begin() + offset + n);
if (!send(transport, opcode, sequence, part, flags)) return false;
}
return true;
}
void LinkService::broadcast(link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body) {
send(LinkTransport::serial, opcode, sequence, body);
if (ble::connected()) send(LinkTransport::ble, opcode, sequence, body);
// Fragmented when needed: a MAPEM indication is larger than one message on either transport.
send_fragmented(LinkTransport::serial, opcode, sequence, body);
if (ble::connected()) send_fragmented(LinkTransport::ble, opcode, sequence, body);
}
void LinkService::reply(LinkTransport transport, std::uint16_t sequence, link::Code code, const link::Bytes& detail) {
send(transport, link::Opcode::RESULT, sequence, link::encode(link::Result {code, detail}));
send_fragmented(transport, link::Opcode::CONFIRM, sequence, link::encode(link::Confirm {code, detail}));
}
link::Status LinkService::status() {
@@ -47,14 +75,58 @@ link::Status LinkService::status() {
value.link_rx_frames = serial_counters.frames + ble_counters.rx_messages;
value.link_crc_errors = serial_counters.crc_errors;
value.link_malformed += ble_counters.malformed;
value.battery_mv = battery_mv_;
value.battery_flags = battery_flags_;
value.battery_mv = battery_millivolts();
return value;
}
link::Code LinkService::provision_segment(const link::CredentialsProvision& c, link::Bytes& detail) {
if (c.offset == 0) { bundle_.clear(); bundle_total_ = c.total_length; }
if (c.total_length != bundle_total_ || c.offset != bundle_.size() || c.total_length > 8192) {
bundle_.clear();
return link::Code::malformed;
}
bundle_.insert(bundle_.end(), c.segment.begin(), c.segment.end());
if (bundle_.size() < bundle_total_) return link::Code::accepted;
link::ApplyReport report;
link::Code result = link::Code::accepted;
try {
result = station_.provision(bundle_, report);
} catch (const std::exception& e) {
ESP_LOGE(TAG, "provision exception: %s", e.what());
result = link::Code::invalid_argument;
}
bundle_.clear();
detail = {static_cast<std::uint8_t>(report.roots), static_cast<std::uint8_t>(report.authorities),
static_cast<std::uint8_t>(report.tickets)};
return result;
}
void LinkService::handle(const link::Bytes& octets, LinkTransport origin) {
link::Message m;
if (!link::decode(octets, m)) return;
auto joined = request_reassembly_.feed(m, unsigned(origin), esp_timer_get_time() / 1000);
if (request_reassembly_.take_overflow()) {
// Every request gets a CONFIRM, also one too large to join.
ESP_LOGW(TAG, "request 0x%02x exceeds %u octets", unsigned(m.header.opcode), unsigned(link::maximum_request_body));
reply(origin, m.header.sequence, link::Code::resource_limit);
return;
}
if (!joined) return;
m = std::move(*joined);
// Every request gets a CONFIRM: an exception below (e.g. a certificate the codec cannot read) is answered with
// rejected instead of leaving the phone waiting for a reply that never comes.
try {
dispatch(m, origin);
} catch (const std::bad_alloc&) {
ESP_LOGE(TAG, "out of memory handling opcode 0x%02x", unsigned(m.header.opcode));
reply(origin, m.header.sequence, link::Code::resource_limit);
} catch (const std::exception& e) {
ESP_LOGE(TAG, "opcode 0x%02x failed: %s", unsigned(m.header.opcode), e.what());
reply(origin, m.header.sequence, link::Code::rejected);
}
}
void LinkService::dispatch(const link::Message& m, LinkTransport origin) {
const auto seq = m.header.sequence;
using link::Opcode; using link::Code;
switch (m.header.opcode) {
@@ -81,23 +153,8 @@ void LinkService::handle(const link::Bytes& octets, LinkTransport origin) {
case Opcode::CREDENTIALS_PROVISION: {
link::CredentialsProvision c;
if (!link::decode(m.body, c)) return reply(origin, seq, Code::malformed);
if (c.offset == 0) { bundle_.clear(); bundle_total_ = c.total_length; }
if (c.total_length != bundle_total_ || c.offset != bundle_.size() || c.total_length > 8192) { bundle_.clear(); return reply(origin, seq, Code::malformed); }
bundle_.insert(bundle_.end(), c.segment.begin(), c.segment.end());
if (bundle_.size() < bundle_total_) return reply(origin, seq, Code::accepted);
link::ApplyReport report;
link::Code result = Code::accepted;
try {
result = station_.provision(bundle_, report);
} catch (const std::exception& e) {
ESP_LOGE(TAG, "provision exception: %s", e.what());
result = Code::invalid_argument;
}
bundle_.clear();
link::Bytes detail;
detail.push_back(static_cast<std::uint8_t>(report.roots));
detail.push_back(static_cast<std::uint8_t>(report.authorities));
detail.push_back(static_cast<std::uint8_t>(report.tickets));
const auto result = provision_segment(c, detail);
return reply(origin, seq, result, detail);
}
case Opcode::CREDENTIALS_ERASE:
@@ -145,27 +202,102 @@ void LinkService::handle(const link::Bytes& octets, LinkTransport origin) {
return reply(origin, seq, station_.unlock(handle));
}
case Opcode::STATUS_REQUEST:
return send(origin, link::Opcode::STATUS, seq, link::encode(status()));
if (!m.body.empty()) return reply(origin, seq, Code::malformed);
return reply(origin, seq, Code::accepted, link::encode(status()));
case Opcode::RAW_CAPTURE: {
link::Reader r(m.body);
const auto mode = r.u8();
if (!r.done()) return reply(origin, seq, Code::malformed);
const auto result = station_.raw_capture(mode);
if (result == Code::accepted) raw_transport_ = origin; // frames go back over the requesting transport
// measuring over USB: no BLE advertising events while capturing (they blind the shared radio)
if (result == Code::accepted && origin == LinkTransport::serial) ble::set_advertising(!(mode & 0x01));
ESP_LOGI(TAG, "RAW_CAPTURE mode 0x%02x over %s: %d", unsigned(mode), origin == LinkTransport::ble ? "BLE" : "serial", int(result));
return reply(origin, seq, result);
}
case Opcode::PKI_TRUST_BOOTSTRAP:
if (origin != LinkTransport::serial) return reply(origin, seq, Code::rejected);
return reply(origin, seq, station_.pki_trust_bootstrap(m.body));
case Opcode::PKI_TRUST_UPDATE: {
link::Reader reader(m.body);
const auto total = reader.u16(), offset = reader.u16();
const auto segment = reader.rest();
if (!reader.ok() || !total || total > 60000 || segment.empty() || segment.size() > total)
return reply(origin, seq, Code::malformed);
if (offset == 0) { trust_update_.clear(); trust_total_ = total; }
if (total != trust_total_ || offset != trust_update_.size() || segment.size() > total - offset) {
trust_update_.clear();
return reply(origin, seq, Code::malformed);
}
trust_update_.insert(trust_update_.end(), segment.begin(), segment.end());
if (trust_update_.size() < total) return reply(origin, seq, Code::accepted);
const auto result = station_.pki_trust_update(trust_update_);
trust_update_.clear(); trust_update_.shrink_to_fit();
return reply(origin, seq, result);
}
case Opcode::PKI_STATUS: {
if (!m.body.empty()) return reply(origin, seq, Code::malformed);
link::Bytes detail;
const auto result = station_.pki_status(detail);
return reply(origin, seq, result, detail);
}
case Opcode::PKI_IDENTITY: {
link::Bytes detail;
const auto result = station_.pki_identity(detail);
return reply(origin, seq, result, detail);
}
case Opcode::PKI_ENROL_REQUEST:
case Opcode::PKI_AUTH_REQUEST: {
const std::uint8_t kind = m.header.opcode == Opcode::PKI_ENROL_REQUEST ? 1 : 2;
link::Bytes request;
const auto result = station_.pki_request(kind, m.body, request);
reply(origin, seq, result);
if (result == Code::accepted) { // the encrypted request for the phone to relay, same sequence
link::Bytes body {kind};
body.insert(body.end(), request.begin(), request.end());
send_fragmented(origin, Opcode::PKI_REQUEST_DATA, seq, body);
}
return;
}
case Opcode::PKI_CANCEL: {
if (m.body.size() != 1) return reply(origin, seq, Code::malformed);
return reply(origin, seq, station_.pki_cancel(m.body[0]));
}
case Opcode::PKI_RETRY: {
if (m.body.size() != 1) return reply(origin, seq, Code::malformed);
link::Bytes request;
const auto result = station_.pki_retry(m.body[0], request);
reply(origin, seq, result);
if (result == Code::accepted) {
link::Bytes body {m.body[0]};
body.insert(body.end(), request.begin(), request.end());
send_fragmented(origin, Opcode::PKI_REQUEST_DATA, seq, body);
}
return;
}
case Opcode::PKI_RESPONSE: {
link::Reader r(m.body);
const auto kind = r.u8();
const auto total = r.u16();
const auto offset = r.u16();
const auto segment = r.rest();
if (!r.ok() || total == 0 || total > 4096) return reply(origin, seq, Code::malformed);
if (offset == 0) { pki_response_.clear(); pki_total_ = total; pki_kind_ = kind; }
if (total != pki_total_ || kind != pki_kind_ || offset != pki_response_.size()) {
pki_response_.clear();
return reply(origin, seq, Code::malformed);
}
pki_response_.insert(pki_response_.end(), segment.begin(), segment.end());
if (pki_response_.size() < pki_total_) return reply(origin, seq, Code::accepted);
link::Bytes detail;
const auto result = station_.pki_response(kind, pki_response_, detail);
pki_response_.clear();
return reply(origin, seq, result, detail);
}
default:
return reply(origin, seq, Code::unknown_opcode);
}
}
void LinkService::set_battery(std::uint16_t millivolts, std::uint8_t flags) {
battery_mv_ = millivolts;
battery_flags_ = flags;
}
void LinkService::send_status_now() {
last_status_us_ = esp_timer_get_time();
broadcast(link::Opcode::STATUS, ++own_sequence_, link::encode(status()));
}
void LinkService::tick() {
const auto now = esp_timer_get_time();
if (now - last_status_us_ < 1000000) return;
last_status_us_ = now;
broadcast(link::Opcode::STATUS, ++own_sequence_, link::encode(status()));
}
} // namespace microbu