#ifndef SERIAL_COMM_H
#define SERIAL_COMM_H

#include <vector>
#include "CSerialPort/SerialPort.h"
#include "CSerialPort/SerialPortInfo.h"
#include <cstdio>

using namespace itas109;

// Serial port configuration
namespace SerialConfig {
    constexpr int BAUD_RATE = 115200;
    constexpr Parity PARITY_TYPE = Parity::ParityNone;
    constexpr DataBits DATA_BITS = DataBits::DataBits8;
    constexpr StopBits STOP_BITS = StopBits::StopOne;
    constexpr FlowControl FLOW_CTRL = FlowControl::FlowNone;
    constexpr int READ_TIMEOUT_MS = 1000;
}

class SerialComm {
private:
    CSerialPort m_serialPort;
    char m_portName[256];
    bool m_isOpen;

public:
    SerialComm();
    ~SerialComm();

    // Find first ttyUSB device
    bool findFirstTtyUSB();
    
    // Open serial port
    bool openPort();
    
    // Close serial port
    void closePort();
    
    // Send data
    bool sendData(const char* data, size_t length);
    bool sendData(const uint8_t* data, size_t length);
    
    // Receive data
    int receiveData(uint8_t* buffer, size_t maxLength);
    int receiveData(char* buffer, size_t maxLength);
    
    // Check if port is open
    bool isOpen() const { return m_isOpen; }
    
    // Get current port name
    const char* getPortName() const { return m_portName; }
    
    // List all available ports
    static void listAllPorts();
};

#endif // SERIAL_COMM_H