#ifndef COMM_H
#define COMM_H

#include <stdint.h>
#include <unistd.h>
#include <QObject>
#include <QtNetwork>
#include <QAbstractSocket>
#include <QtSerialPort/QSerialPort>
#include "project.h"

//	----------------------------------------------------------------------------
//	commPort_t
//	----------------------------------------------------------------------------

#define RBLEN 8000
#define RXBUFLEN 8000

typedef struct {
	unsigned char rbuf[RBLEN];
	int rbufHead;
	int rbufTail;
} commBuff_t;

//	----------------------------------------------------------------------------
//	Constants
//	----------------------------------------------------------------------------

enum {
	CONNECT_NONE = 0,
	CONNECT_232,
	CONNECT_LAN,
	CONNECT_FAIL,
};

//	----------------------------------------------------------------------------
//	comm
//	----------------------------------------------------------------------------

class comm : public QObject
{
	Q_OBJECT

public:

//	----------------------------------------------------------------------------
//	Construct/destruct
//	----------------------------------------------------------------------------

	explicit comm(QTcpSocket *socket,QSerialPort *serial,QObject *parent = 0);

//	----------------------------------------------------------------------------
//	Interface
//	----------------------------------------------------------------------------

public:

	int closePort();
	int openPort(const char *portNamePar,int baudRatePar, int portPar);
	int getBaudRate();
//	int getPortState();
	int isConnected();
	char *getPortName();
	char *getPortMessage();
//	int moduleSendMsgWait();
//	int moduleSendMsg();
//	int readPort(unsigned char *buffer, int maxLen, int *errorCode, int *morebytes);

//	----------------------------------------------------------------------------
//	Internal
//	----------------------------------------------------------------------------

private:

	void ReadLAN(unsigned char *rbuf,unsigned int bytesToRead,unsigned int *bytesRead);
	void Read232(unsigned char *rbuf,unsigned int bytesToRead,unsigned int *bytesRead);
	void WriteLAN(unsigned char *tbuf,int bytesToWrite, int *bytesWritten);
	void Write232(unsigned char *tbuf,int bytesToWrite, int *bytesWritten);
	int openLanPort(const char *portNamePar, int portPar);
	int closeLanPort();
	int open232Port(const char *portNamePar,int baudRatePar);
	int close232Port();
	int commandCheck(msgHeader_t *h, uint32_t *len, uint32_t maxlen);
	int checkSumX(int len, int *sum1,int *sum2);

public:
	int readByte(char *c);
	void writePort(unsigned char *bufferPtr, int len);
	int getMessage();

//	const char *cmdToStrPtr(unsigned cmd);
//	const char *respToStrPtr(unsigned resp);

//	----------------------------------------------------------------------------
//	signals & slots
//	----------------------------------------------------------------------------

public:
signals:
	void error(int comError, const QString &message);
private slots:

//	----------------------------------------------------------------------------
//	attributes
//	----------------------------------------------------------------------------

public:
	QTcpSocket *sockClient;
	QSerialPort *serialPort;
private:

	int sockInit;
	unsigned int  nPort;
	int lanPortOpen;
	int serialPortOpen;
	int portHandle;
	int baudRate;
	char portMessage[250];
	char portName[250];
	int goodCount;
	int crcCount;
	int maxlenCount;
	int nodataCount;
	int gimbalTimeout;
	int gimbalConnected;
	int getMsgCount;
	int quit;
	commBuff_t commBuff;

public:
	unsigned char message[RXBUFLEN];
	int wait;
private:
	int16_t oldCount;
	int id;
	uint32_t len;
	uint32_t index;
	int state;
	int sum1;
	int sum2;
	int err1;
	int err2;
	int err3;
	int good;
	int fpass;

public:
	int connected;
	int gimbalPresent;
	int errorCode;
	int verbose;
	int totalcount;
};

#endif // COMM_H
