Files
MicrOBU/obu-firmware/main/link_service.hpp
T
Ashin Walpola 50bbf6552d 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.
2026-11-11 00:13:57 +01:00

71 lines
3.8 KiB
C++

#pragma once
// Dispatches station-link messages (phone -> micrOBU) to the station and sends the
// micrOBU -> phone messages (confirmations, indications and identifier-change events).
#include "link_protocol.hpp"
#include "request_reassembly.hpp"
#include "station.hpp"
#include "battery_monitor.hpp"
namespace microbu {
/// @brief Represents the transport mechanism for link messages. Either BLE or Serial
enum class LinkTransport : std::uint8_t { serial, ble };
class LinkService {
public:
/// @brief Binds the service to a station and subscribes to its indication/id-event callbacks.
/// @param station Station to bind to; must outlive the LinkService.
explicit LinkService(Station& station);
/// @brief Decodes and dispatches one received message; call from the station task.
/// @param message Raw message bytes as received from the transport.
/// @param origin Transport the message arrived on; replies go back on the same transport.
void handle(const link::Bytes& message, LinkTransport origin);
/// @brief MicrOBU: one calibrated cell reading in mV (0: not measured), for the battery guard.
std::uint16_t battery_millivolts() { return battery_.sample_millivolts(); }
private:
void dispatch(const link::Message& message, LinkTransport origin);
/// @brief Encodes and writes one message on the given transport.
/// @param transport Transport to write to.
/// @param opcode Message opcode.
/// @param sequence Sequence number to stamp on the header.
/// @param body Encoded message body.
bool send(LinkTransport transport, link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body,
std::uint8_t flags = 0);
/// @brief Sends a body of any size, split into fragments of the transport's message size
/// (README "Fragmentation": full header and sequence on each fragment).
bool send_fragmented(LinkTransport transport, link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body);
/// @brief Sends a message on serial, and also on BLE when a phone is connected.
/// @param opcode Message opcode.
/// @param sequence Sequence number to stamp on the header.
/// @param body Encoded message body.
void broadcast(link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body);
/// @brief Sends a CONFIRM reply to the message's origin transport.
/// @param origin Transport to reply on.
/// @param sequence Sequence number of the message being replied to.
/// @param code Result code to report.
/// @param detail Optional result detail bytes.
void reply(LinkTransport origin, std::uint16_t sequence, link::Code code, const link::Bytes& detail = {});
/// @brief Appends one CREDENTIALS_PROVISION segment and provisions the bundle once it is complete.
/// @param segment Decoded segment.
/// @param detail Receives the applied root/authority/ticket counts after the last segment.
/// @return accepted for a stored segment, else the malformed or provisioning result.
link::Code provision_segment(const link::CredentialsProvision& segment, link::Bytes& detail);
/// @brief Builds a STATUS snapshot from the station and transport counters.
/// @return Current status.
link::Status status();
Station& station_;
BatteryMonitor battery_;
link::RequestReassembly request_reassembly_;
std::uint16_t own_sequence_ = 0;
link::Bytes trust_update_;
std::uint16_t trust_total_ = 0;
link::Bytes bundle_; // credentials being provisioned
std::uint16_t bundle_total_ = 0;
link::Bytes pki_response_; // an EA/AA response arriving in segments (PKI_RESPONSE)
std::uint16_t pki_total_ = 0;
std::uint8_t pki_kind_ = 0;
LinkTransport raw_transport_ = LinkTransport::serial; // where RAW_CAPTURE was switched on
};
} // namespace microbu