#ifndef LRUSERIALPORT_H
#define LRUSERIALPORT_H

#include <QSerialPort>
#include "common_def.h"


class LruSerialPort;
class UnitMonitor;

class LruSerialFilter
{
public:
    virtual bool serialFilterIn(LruSerialPort& port, QByteArray& data)=0;
    virtual bool processMessages( QByteArray& data, UnitMonitor* umon)=0;
};

class LruSerialPort : public QObject //public QSerialPort
{
    Q_OBJECT

public:
    LruSerialPort(const QString&name, deviceID device, QSerialPort::BaudRate baud=QSerialPort::Baud9600, QSerialPort::Parity parity=QSerialPort::EvenParity);

    void close();

    QString portName() const { return port_name;}

    void setUnitMonitor(UnitMonitor* m) { unitMonitor=m;}

    void setEnabled(bool enable);

    void setFilter(LruSerialFilter* filter);

    QByteArray readAll();

    QByteArray readData();

    qint64 write(const QByteArray& data);

    void writeChar(char c);

    void flush();

    void attachUserInfo(void* info) { user_info=info;}
    void* userInfo() const { return user_info;}

signals:
    void readyRead();

signals:
    void asynchReadyRead(unsigned int index, const char* data, int len, unsigned int comm_event);

private:
    //QSerialPort realPort;

public:

    QString port_name;

    int portId;

    bool realPortOk;

    LruSerialFilter* inFilter;
    UnitMonitor* unitMonitor;

    bool enabled;

    unsigned long chars_in;
    unsigned long chars_out;

    deviceID numDevice;

    QByteArray tempBuffer;

    void* user_info;

private:
    class Implementation;
    Implementation& p_;
};

#endif // LRUSERIALPORT_H
