/*
{{copyright}} 
*/

/*
{{version}} 
*/

/*
{{license}} 
*/

#pragma once

#include "../UARTBus.h"

#include <sdi_uToolBox/generic/FreeRTOS/mutex.h>

namespace sdi_uToolBox::stm32::comm::FreeRTOS
{
//--------------------------------------------------------------
class UARTBus : public stm32::comm::UARTBus
{
  public:
  using Mutex = generic::FreeRTOS::Mutex;

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

  ReturnStatus open() override;      // Open UART bus
  ReturnStatus close() override;     // Close UART bus

  ReturnType write(WriteBuf buf, Timeout timeout) const override;     // Write data to UART device
  ReturnType read(ReadBuf buf, Timeout timeout) const override;       // Read data from UART device

  protected:
  mutable Mutex m_mtx;     // Mutex for thread safety
};
//--------------------------------------------------------------
/* Open UART bus */
inline ReturnStatus UARTBus::open()
{
  std::scoped_lock lock(m_mtx);
  return stm32::comm::UARTBus::open();
}
//--------------------------------------------------------------
/* Close UART bus */
inline ReturnStatus UARTBus::close()
{
  std::scoped_lock lock(m_mtx);
  return stm32::comm::UARTBus::close();
}
//--------------------------------------------------------------
/* Write data to UART device */
inline UARTBus::ReturnType UARTBus::write(const WriteBuf buf, const Timeout timeout) const
{
  std::scoped_lock lock(m_mtx);
  return stm32::comm::UARTBus::write(buf, timeout);
}
//--------------------------------------------------------------
/* Read data from UART device */
inline UARTBus::ReturnType UARTBus::read(const ReadBuf buf, const Timeout timeout) const
{
  std::scoped_lock lock(m_mtx);
  return stm32::comm::UARTBus::read(buf, timeout);
}
//--------------------------------------------------------------
}     // namespace sdi_uToolBox::stm32::comm::FreeRTOS
