2026-09-23 17:27:54 +02:00
|
|
|
#pragma once
|
|
|
|
|
// Dispatches station-link messages (phone -> micrOBU) to the station and sends the
|
2026-11-11 00:13:57 +01:00
|
|
|
// micrOBU -> phone messages (confirmations, indications and identifier-change events).
|
2026-09-23 17:27:54 +02:00
|
|
|
#include "link_protocol.hpp"
|
2026-11-11 00:13:57 +01:00
|
|
|
#include "request_reassembly.hpp"
|
2026-09-23 17:27:54 +02:00
|
|
|
#include "station.hpp"
|
2026-11-11 00:13:57 +01:00
|
|
|
#include "battery_monitor.hpp"
|
2026-09-23 17:27:54 +02:00
|
|
|
|
|
|
|
|
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);
|
2026-11-11 00:13:57 +01:00
|
|
|
/// @brief MicrOBU: one calibrated cell reading in mV (0: not measured), for the battery guard.
|
|
|
|
|
std::uint16_t battery_millivolts() { return battery_.sample_millivolts(); }
|
2026-09-23 17:27:54 +02:00
|
|
|
private:
|
2026-11-11 00:13:57 +01:00
|
|
|
void dispatch(const link::Message& message, LinkTransport origin);
|
2026-09-23 17:27:54 +02:00
|
|
|
/// @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.
|
2026-11-11 00:13:57 +01:00
|
|
|
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);
|
2026-09-23 17:27:54 +02:00
|
|
|
/// @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);
|
2026-11-11 00:13:57 +01:00
|
|
|
/// @brief Sends a CONFIRM reply to the message's origin transport.
|
2026-09-23 17:27:54 +02:00
|
|
|
/// @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 = {});
|
2026-11-11 00:13:57 +01:00
|
|
|
/// @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);
|
2026-09-23 17:27:54 +02:00
|
|
|
/// @brief Builds a STATUS snapshot from the station and transport counters.
|
|
|
|
|
/// @return Current status.
|
|
|
|
|
link::Status status();
|
|
|
|
|
Station& station_;
|
2026-11-11 00:13:57 +01:00
|
|
|
BatteryMonitor battery_;
|
|
|
|
|
link::RequestReassembly request_reassembly_;
|
2026-09-23 17:27:54 +02:00
|
|
|
std::uint16_t own_sequence_ = 0;
|
2026-11-11 00:13:57 +01:00
|
|
|
link::Bytes trust_update_;
|
|
|
|
|
std::uint16_t trust_total_ = 0;
|
2026-09-23 17:27:54 +02:00
|
|
|
link::Bytes bundle_; // credentials being provisioned
|
|
|
|
|
std::uint16_t bundle_total_ = 0;
|
2026-11-11 00:13:57 +01:00
|
|
|
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
|
2026-09-23 17:27:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace microbu
|