52 lines
2.4 KiB
C++
52 lines
2.4 KiB
C++
#pragma once
|
|||
|
|
// Dispatches station-link messages (phone -> micrOBU) to the station and sends the
|
||
|
|
// micrOBU -> phone messages (results, indications, identifier-change events, status).
|
||
|
|
#include "link_protocol.hpp"
|
||
|
|
#include "station.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 Periodic work: broadcasts STATUS once a second.
|
||
|
|
void tick();
|
||
|
|
private:
|
||
|
|
/// @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.
|
||
|
|
void send(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 RESULT 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 Builds a STATUS snapshot from the station and transport counters.
|
||
|
|
/// @return Current status.
|
||
|
|
link::Status status();
|
||
|
|
Station& station_;
|
||
|
|
std::uint16_t own_sequence_ = 0;
|
||
|
|
link::Bytes bundle_; // credentials being provisioned
|
||
|
|
std::uint16_t bundle_total_ = 0;
|
||
|
|
std::int64_t last_status_us_ = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace microbu
|