#pragma once

#include <cstdint>

#include <errno.h>
#include <pthread.h>

#define RECV_BUFF_SIZE 1024

class CComm
{
public:
	CComm();
	virtual ~CComm();

	virtual bool Open(void *p) = 0;
	virtual void Close() = 0;

	virtual error_t Receive() = 0;
	virtual size_t Transmit(const uint8_t *buf, size_t toWriteBytes) = 0;

	size_t GetReceiveData(uint8_t *buf);
	size_t GetReceiveDataSize();
	void ClearReceiveBuffer();

protected:
	struct Buffer
	{
		uint8_t data[RECV_BUFF_SIZE];
		size_t write_pos;
		size_t read_pos;
		size_t mask;
	};

	void WriteBuffer(uint8_t *data, size_t size);
	size_t ReadBuffer(uint8_t *buf);

	pthread_t thread_;
	pthread_mutex_t mutex_;
	Buffer buffer_;
};
