//..............................................................................
//
// Modbus stream parser base class
//

enum ModbusParserMode
{
	Master,
	Slave,
	HalfDuplex,
}

class ModbusParserBase
{
protected:
	std.Buffer m_dataBuffer;
	io.ModbusRole m_modbusRole;
	uint64_t m_lastTimestamp;

public:
	io.ModbusProtocol readonly m_protocol;
	ModbusParserMode autoget property m_mode;
	uint64_t m_halfDuplexTimeout; // in 100-nsec intervals

public:
	abstract parse(
		log.Writer* writer,
		uint64_t timestamp,
		uint64_t originalRecordCode,
		void const* p,
		size_t size
		);

	abstract reset();

protected:
	updateLastTimestamp(uint64_t timestamp);
}

// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ModbusParserBase.m_mode.set(ModbusParserMode mode)
{
	if (mode == m_value)
		return;

	m_value = mode;

	m_modbusRole = mode == ModbusParserMode.Slave ?
		io.ModbusRole.Slave :
		io.ModbusRole.Master;

	reset();
}

ModbusParserBase.updateLastTimestamp(uint64_t timestamp)
{
	if (m_mode == ModbusParserMode.HalfDuplex &&
		timestamp - m_lastTimestamp > m_halfDuplexTimeout)
		reset();

	m_lastTimestamp = timestamp;
}

//..............................................................................
