Files
MicrOBU/obu-firmware/main/link_service.hpp
T
Ashin Walpola ce18a393ea Fragment large V2X_RX messages and decode road-RSU SPATEMs
The firmware dropped every received ITS message over 494 bytes (one link
message), so a road RSU's 888-byte SPATEM and 1198-byte MAPEM never reached
the phone. V2X_RX now goes out in link fragments up to 2300 bytes, best
effort and non-blocking; messages up to 494 bytes are unchanged.

The app's SPATEM decoder then refused those messages because they carry
intersection and movement names and advisory speed lists. It now reads past
both, checked against asn1tools on 718 distinct messages.

Adds the pure fragmenter with host tests and a pcap replay, a Kotlin
cross-check on the firmware's own fragments, tools/rx_size_compare.py,
docs/08-large-message-reception.md and station-link revision 3.
2026-11-23 17:34:56 +01:00

74 lines
4.0 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.
/// @return whether at least one transport took the whole message (the caller only counts a false).
bool broadcast(link::Opcode opcode, std::uint16_t sequence, const link::Bytes& body);
/// Most fragments one received ITS message (V2X_RX) may take on a transport; 8 x 504 octets at the full MTU.
static constexpr std::size_t max_received_fragments = 8;
/// @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