// =============================================================================
//  modbus_slave.h -- Modbus RTU server (slave) interface
// =============================================================================
//
//  Sprint 1: exposes the read-only telemetry registers (input registers and
//  discrete inputs) and accepts writes to the holding registers and coils
//  defined in modbus_registers.h (auto-generated).
//
//  Reads come straight from the live telemetry helpers
//  (modes::current_mode(), hal::rudder_sensor_latest(), etc.).
//
//  Writes are validated and routed to the corresponding command handler
//  (mode change, alarm ack, etc.). Invalid values are rejected with
//  ILLEGAL_DATA_VALUE per Modbus convention.
//
//  All Modbus internals (RX task, response framing, RS-485 DE/RE control)
//  are delegated to the eModbus library, which spawns its own tasks on
//  Core 0.
// =============================================================================

#pragma once

#include <cstdint>

namespace arautopilot::protocols::modbus {

/// Initialise the RS-485 UART, configure the Modbus server, and register
/// every read/write callback. Must be called from setup() after di_init()
/// and rudder_sensor_init().
void modbus_slave_init();

/// Start the eModbus server. After this returns, the slave is listening on
/// the configured UART/baudrate and replies asynchronously.
void modbus_slave_start();

/// True after modbus_slave_start() has successfully initialised the server.
bool modbus_slave_is_running();

/// Number of valid requests processed since boot (cheap performance counter).
uint32_t modbus_slave_request_count();

/// Number of malformed requests / illegal-data-value rejections since boot.
uint32_t modbus_slave_error_count();

}  // namespace arautopilot::protocols::modbus
