//{{copyright}}

//{{version}}

//{{license}}

#pragma once

#include "ICommBus.h"

#include <chrono>
#include <expected>
#include <span>

namespace sdi_uToolBox::comm
{
    //--------------------------------------------------------------
    class SerialBus : public ICommBus
    {
    public:
        using ReadBuf = std::span<uint8_t>;
        using WriteBuf = std::span<const uint8_t>;
        using Timeout = std::chrono::milliseconds;
        using ReturnType = std::expected<size_t, ReturnStatus>;

    public:
        SerialBus() = default; // Default constructor
        virtual ~SerialBus() override = default; // Default destructor
        SerialBus(const SerialBus&) = delete; // Copy constructor
        SerialBus(SerialBus&&) = delete; // Move constructor
        SerialBus& operator=(const SerialBus&) = delete; // Copy assignment operator
        SerialBus& operator=(SerialBus&&) = delete; // Move assignment operator

        virtual ReturnType write(WriteBuf buf, Timeout timeout) const = 0; // Write data to UART device
        virtual ReturnType read(ReadBuf buf, Timeout timeout) const = 0; // Read data from UART device
        virtual ReturnType readUntilIdle(ReadBuf buf, Timeout timeout) const; // Read data until IDLE event
    };

    //--------------------------------------------------------------
    /*  Read data until IDLE event */
    inline SerialBus::ReturnType SerialBus::readUntilIdle(ReadBuf buf, Timeout timeout) const
    {
        return std::unexpected(ReturnStatus::IncompatibleBaseType);
    }

    //--------------------------------------------------------------
} // namespace sdi_uToolBox::comm
