#ifndef PORTCONTROLLER_H
#define PORTCONTROLLER_H

// only one defined at time
#define USE_LIB_USB
//#define USE_SERIAL

#include "decoder.h"
#include <QObject>
#include <QThread>

#ifdef USE_LIB_USB
#include "LIBUSBport.h"
#include "libusb.h"
#endif

#ifdef USE_SERIAL
#include "SerialPort.h"
#endif

class PortController : public QObject {
  Q_OBJECT
  QThread portThread; // port thread

public:
  PortController(Decoder *d);
  ~PortController();

  QList<QString> getAvailablePorts();           // get available ports
  void openPort(QString portName);              // open port
  bool getOpened() { return this->portOpened; } // port opened
  void sendData(QByteArray data);               // send data request
  void stopPort();                              // stop port

private:
  volatile bool portOpened; // port opened
#ifdef USE_LIB_USB
  QList<libusb_device *> devices; // available devices list
  libusb_device **devs;           // devices from libusb
#endif
public slots:
  void opened(bool);
signals:
#ifdef USE_LIB_USB
  void open(libusb_device *); // open libusb port
#endif
#ifdef USE_SERIAL
  void open(QString); // open serial port
#endif
  void send(QByteArray);       // send data
  void stop();                 // stop port
  void openStatus(bool, bool); // open status (now, previous)
};

#endif // PORTCONTROLLER_H
