//! \~\file pican.h
//! \~\ingroup IO
//! \~\brief
//! \~english CAN bus device wrapper
//! \~russian Обертка над устройством шины CAN
/*
    PIP - Platform Independent Primitives
    CAN
    Andrey Bychkov work.a.b@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef PICAN_H
#define PICAN_H

#include "piiodevice.h"


//! \~\ingroup IO
//! \~\brief
//! \~english CAN device based on interface name and frame identifier.
//! \~russian CAN-устройство, настраиваемое именем интерфейса и идентификатором кадра.
class PIP_EXPORT PICAN: public PIIODevice {
	PIIODEVICE(PICAN, "can");

public:
	//!  \~english Constructs a CAN device for interface "path".
	//! \~russian Создает CAN-устройство для интерфейса "path".
	explicit PICAN(const PIString & path = PIString(), PIIODevice::DeviceMode mode = PIIODevice::ReadWrite);

	//! \~english Destroys the CAN device.
	//! \~russian Уничтожает CAN-устройство.
	virtual ~PICAN();

	//! \~english Sets CAN frame identifier for subsequent \a write() calls.
	//! \~russian Устанавливает идентификатор CAN-кадра для последующих вызовов \a write().
	void setCANID(int id);

	//! \~english Returns CAN frame identifier used by \a write().
	//! \~russian Возвращает идентификатор CAN-кадра, используемый методом \a write().
	int CANID() const;

	//! \~english Returns identifier of the last frame received by \a read().
	//! \~russian Возвращает идентификатор последнего кадра, полученного методом \a read().
	int readedCANID() const;

	//! \~english Interrupts a blocking CAN wait operation.
	//! \~russian Прерывает блокирующее ожидание CAN-кадра.
	void interrupt() override;

protected:
	bool openDevice() override;
	bool closeDevice() override;
	ssize_t readDevice(void * read_to, ssize_t max_size) override;
	ssize_t writeDevice(const void * data, ssize_t max_size) override;
	PIString constructFullPathDevice() const override;
	void configureFromFullPathDevice(const PIString & full_path) override;
	PIPropertyStorage constructVariantDevice() const override;
	void configureFromVariantDevice(const PIPropertyStorage & d) override;
	DeviceInfoFlags deviceInfoFlags() const override { return PIIODevice::Reliable; }

private:
	PRIVATE_DECLARATION(PIP_EXPORT)
	int sock = -1;
	int can_id, readed_id;
};

#endif // PICAN_H
