#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