//..............................................................................
//
// Serial Monitor session
//

import "io_Monitor.jnc"

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

class SerialMonitor: io.Monitor
{
protected:
	SerialMonSession* m_session;
	io.SerialStatusLines m_statusLines;

public:
	construct (SerialMonSession* session);

	bool errorcode capture (char const* deviceName);

protected:
	override processNotification (io.DeviceMonitorNotifyHdr const* notifyHdr);
	override processError (std.Error const* error);

	processNotification_win (io.DeviceMonitorNotifyHdr const* notifyHdr);
	processNotification_lnx (io.DeviceMonitorNotifyHdr const* notifyHdr);

	processIoctlNotification_win (
		int ntStatus,
		uint64_t timestamp,
		uint_t code,
		const void* inData,
		size_t inDataSize,
		const void* outData,
		size_t outDataSize
		);

	processIoctlNotification_lnx (
		int result,
		uint64_t timestamp,
		uint_t code,
		long arg,
		const void* argData,
		size_t argDataSize
		);
}

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

SerialMonitor.construct (SerialMonSession* session)
{
	basetype.construct (session.m_pluginHost);
	m_session = session;
}

bool errorcode SerialMonitor.capture (char const* deviceName)
{
	static io.psx.DeviceMonitorIoctlDesc ioctlDescTable [] =
	{
		{ psx.TCSETS, sizeof (psx.termios) },
		{ psx.TIOCMGET, sizeof (uint_t) },
		{ psx.TIOCMBIS, sizeof (uint_t) },
		{ psx.TIOCMBIC, sizeof (uint_t) },
	}

	basetype.capture (deviceName, null, ioctlDescTable, sizeof (ioctlDescTable) / sizeof (ioctlDescTable [0]));
	m_statusLines = (io.SerialStatusLines) -1; // special meaning: unintitalized
	return true;
}

SerialMonitor.processNotification (io.DeviceMonitorNotifyHdr const* notifyHdr)
{
	switch (sys.g_systemInfo.m_osKind)
	{
	case sys.OsKind.Windows:
		processNotification_win (notifyHdr);
		break;

	case sys.OsKind.Linux:
		processNotification_lnx (notifyHdr);
		break;
	}
}

SerialMonitor.processError (std.Error const* error)
{
	m_pluginHost.m_log.m_writer.write (log.StdRecordCode.Error, error, error.m_size);
	m_session.close ();
}

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