//..............................................................................
//
// Serial log record codes & params
//

import "log_RecordCode.jnc"
import "io_base.jncx"
import "io_SerialPortEnumerator.jnc"
import "std_Buffer.jnc"

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

enum SerialLogRecordCode: uint64_t {
	PortOpened            = 0x01d47c8fc7b0f700 | log.RecordCodeFlags.Foldable,
	PortOpened_old        = 0x01d47c8fc7b0f701 | log.RecordCodeFlags.Foldable,
	PortOpenError         = 0x01d47c8fc7b0f702,
	PortClosedNamed       = 0x01d47c8fc7b0f703,
	BaudRateChanged       = 0x01d47c8fc7b0f704,
	DataBitsChanged       = 0x01d47c8fc7b0f705,
	StopBitsChanged       = 0x01d47c8fc7b0f706,
	ParityChanged         = 0x01d47c8fc7b0f707,
	FlowControlChanged    = 0x01d47c8fc7b0f708,
	RtsChanged            = 0x01d47c8fc7b0f709,
	DtrChanged            = 0x01d47c8fc7b0f70a,
	StatusLineChanged     = 0x01d47c8fc7b0f70b,
	ReadModeChanged       = 0x01d47c8fc7b0f70c,
	ReadIntervalChanged   = 0x01d47c8fc7b0f70d,
	BuggyDriverDetected   = 0x01d47c8fc7b0f70e,
	ControlLineChanged    = 0x01d47c8fc7b0f70f,
	LineError             = 0x01d47c8fc7b0f710,
	BreakConditionChanged = 0x01d47c8fc7b0f711,
	PortClosed            = 0x01d47c8fc7b0f712,
	PortOpenedEx          = 0x01d47c8fc7b0f713 | log.RecordCodeFlags.Foldable,
	CaptureStarted        = 0x01d47c8fc7b0f714 | log.RecordCodeFlags.Foldable,
}

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

enum SerialReadMode {
	CheckComstat,
	WaitFirstChar,
	IntervalBased
}

struct SerialOpenParams {
	uint_t m_baudRate;
	uint_t m_flowControl;
	uint_t m_dataBits;
	uint_t m_stopBits;
	uint_t m_parity;
	uint_t m_readInterval;
	bool m_dtr;
	bool m_rts;
	uint_t m_statusLines;
	uint_t m_options;
	uint_t m_osKind;

	// followed by port name

	// for SerialLogRecordCode.PortOpenedEx, also followed by
	// - description
	// - manufacturer
	// - hardwareIds
	// - driver
	// - location
}

struct SerialOpenParams_old {
	uint_t m_baudRate;
	uint_t m_flowControl;
	uint_t m_dataBits;
	uint_t m_stopBits;
	uint_t m_parity;

	// followed by port name
}

struct SerialStatusLineChangedParams {
	io.SerialStatusLines m_lines;
	io.SerialStatusLines m_mask;
}

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

io.SerialPortDesc const* findSerialPortDesc(string_t deviceName) {
	io.SerialPortDesc const* portList = io.enumerateSerialPorts(io.SerialPortDescFlags.All);
	for (io.SerialPortDesc const* port = portList; port; port = port.m_next)
		if (port.m_deviceName == deviceName)
			return port;


	return null;
}

void packSerialPortDescStrings(
	std.Buffer* buffer,
	io.SerialPortDesc const* portDesc
) {
	buffer.append(portDesc.m_deviceName.m_p, portDesc.m_deviceName.m_length);
	buffer.append(0);
	buffer.append(portDesc.m_description.m_p, portDesc.m_description.m_length);
	buffer.append(0);
	buffer.append(portDesc.m_manufacturer.m_p, portDesc.m_manufacturer.m_length);
	buffer.append(0);
	buffer.append(portDesc.m_hardwareIds.m_p);
	buffer.append(0);
	buffer.append(portDesc.m_driver.m_p, portDesc.m_driver.m_length);
	buffer.append(0);
	buffer.append(portDesc.m_location.m_p, portDesc.m_location.m_length);
	buffer.append(0);
}

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