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:
@@ -1,14 +1,14 @@
|
||||
#include "serial_link.hpp"
|
||||
#include "sdkconfig.h"
|
||||
#include <driver/usb_serial_jtag.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_timer.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
#include <algorithm>
|
||||
#include <atomic>
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <atomic>
|
||||
#include <cstring>
|
||||
|
||||
namespace microbu::serial {
|
||||
@@ -77,25 +77,26 @@ namespace {
|
||||
SemaphoreHandle_t writer_lock = nullptr;
|
||||
Counters the_counters;
|
||||
Decoder::Handler frame_handler;
|
||||
std::atomic<std::uint32_t> last_frame_at_ms {0}; // 32-bit: no libatomic needed on RV32
|
||||
#if CONFIG_MICROBU_LOG_OVER_LINK
|
||||
vprintf_like_t previous_vprintf = nullptr;
|
||||
#endif
|
||||
|
||||
bool write_all(const Bytes& frame) {
|
||||
// A host that has the port enumerated but does not read lets the TX buffer fill. Waiting 500 ms per
|
||||
// frame then stalled every logging task, the BLE host included, and delayed the phone's requests by a
|
||||
// minute (2026-10-05). After a timeout the serial side counts as stalled: frames are dropped at once,
|
||||
// one short probe per second finds a host that reads again, and any received byte ends the stall.
|
||||
std::atomic<bool> stalled {false};
|
||||
std::atomic<std::uint32_t> last_frame_at_ms {0}; // MicrOBU: 32-bit, no libatomic needed on RV32
|
||||
std::atomic<std::int64_t> next_probe_us {0};
|
||||
|
||||
bool write_all(const Bytes& frame, TickType_t timeout) {
|
||||
std::size_t sent = 0;
|
||||
while (sent < frame.size()) {
|
||||
// MicrOBU: 50 ms, not the colleague's 500. A phone that is plugged in but not reading (app
|
||||
// not running) fills the driver's buffer, and every write then waits out this timeout on
|
||||
// the station task, which also serves BLE and the radio.
|
||||
const int count = usb_serial_jtag_write_bytes(frame.data() + sent, frame.size() - sent, pdMS_TO_TICKS(50));
|
||||
const int count = usb_serial_jtag_write_bytes(frame.data() + sent, frame.size() - sent, timeout);
|
||||
if (count <= 0) return false; // the host detects an incomplete frame by CRC
|
||||
sent += count;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#if CONFIG_MICROBU_LOG_OVER_LINK
|
||||
// ESP_LOG sink: one LOG frame per call, never a raw write into the frame stream.
|
||||
int log_to_frame(const char* format, va_list args) {
|
||||
char line[256];
|
||||
@@ -104,11 +105,10 @@ int log_to_frame(const char* format, va_list args) {
|
||||
std::size_t n = static_cast<std::size_t>(length) < sizeof line ? length : sizeof line - 1;
|
||||
while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r')) --n;
|
||||
if (n == 0) return length;
|
||||
// esp_log colour escapes are stripped by the emulator; keep the line verbatim otherwise.
|
||||
// esp_log colour escapes are stripped by the host; keep the line verbatim otherwise.
|
||||
write(FrameType::LOG, Bytes(line, line + n));
|
||||
return length;
|
||||
}
|
||||
#endif
|
||||
|
||||
void reader_task(void*) {
|
||||
Decoder decoder;
|
||||
@@ -116,6 +116,7 @@ void reader_task(void*) {
|
||||
for (;;) {
|
||||
const int count = usb_serial_jtag_read_bytes(buffer, sizeof buffer, pdMS_TO_TICKS(20));
|
||||
if (count > 0) {
|
||||
stalled = false;
|
||||
decoder.feed(buffer, static_cast<std::size_t>(count), [](Frame frame) {
|
||||
last_frame_at_ms = static_cast<std::uint32_t>(esp_timer_get_time() / 1000);
|
||||
frame_handler(std::move(frame));
|
||||
@@ -134,25 +135,40 @@ void start(const Decoder::Handler& on_frame) {
|
||||
config.rx_buffer_size = 8192;
|
||||
config.tx_buffer_size = 8192;
|
||||
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&config));
|
||||
#if CONFIG_MICROBU_LOG_OVER_LINK
|
||||
previous_vprintf = esp_log_set_vprintf(log_to_frame);
|
||||
#endif
|
||||
xTaskCreate(reader_task, "link_rx", 6144, nullptr, 12, nullptr);
|
||||
}
|
||||
|
||||
bool write(FrameType type, const Bytes& payload) {
|
||||
if (payload.size() > maximum_payload || !writer_lock) return false;
|
||||
// MicrOBU: nothing on the native port (phone on BLE, or unplugged). Without this check every
|
||||
// write, the 1 Hz STATUS included, would block the station task until the timeout.
|
||||
if (!usb_serial_jtag_is_connected()) { ++the_counters.not_connected; return false; }
|
||||
// MicrOBU: nothing on the native port (phone on BLE, or unplugged): do not even try, or the first
|
||||
// write after every pause waits out the timeout on the station task.
|
||||
if (!usb_serial_jtag_is_connected()) { ++the_counters.write_failures; return false; }
|
||||
TickType_t timeout = pdMS_TO_TICKS(500);
|
||||
if (stalled) {
|
||||
const auto now = esp_timer_get_time();
|
||||
if (now < next_probe_us) { ++the_counters.write_failures; return false; }
|
||||
next_probe_us = now + 1000000;
|
||||
timeout = std::max<TickType_t>(1, pdMS_TO_TICKS(20));
|
||||
}
|
||||
const auto frame = encode_frame(type, payload);
|
||||
if (xSemaphoreTake(writer_lock, pdMS_TO_TICKS(1000)) != pdTRUE) { ++the_counters.write_failures; return false; }
|
||||
const bool ok = write_all(frame);
|
||||
if (xSemaphoreTake(writer_lock, stalled ? 0 : pdMS_TO_TICKS(1000)) != pdTRUE) { ++the_counters.write_failures; return false; }
|
||||
const bool ok = write_all(frame, timeout);
|
||||
stalled = !ok;
|
||||
xSemaphoreGive(writer_lock);
|
||||
if (!ok) ++the_counters.write_failures;
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool try_write(FrameType type, const Bytes& payload) {
|
||||
if (payload.size() > maximum_payload || !writer_lock || stalled) return false;
|
||||
const auto frame = encode_frame(type, payload);
|
||||
if (xSemaphoreTake(writer_lock, 0) != pdTRUE) return false;
|
||||
const bool queued = usb_serial_jtag_write_bytes(frame.data(), frame.size(), 0) == static_cast<int>(frame.size());
|
||||
xSemaphoreGive(writer_lock);
|
||||
return queued;
|
||||
}
|
||||
|
||||
Counters counters() { return the_counters; }
|
||||
|
||||
std::uint32_t last_frame_ms() { return last_frame_at_ms.load(); }
|
||||
|
||||
Reference in New Issue
Block a user