//..............................................................................
//
// Fall-back settings for buggy serial port drivers
//

import "io_base.jncx"
import "io_Serial.jnc"
import "io_File.jnc"

namespace io {

//! \addtogroup common-serial
//! @{

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

char const* detectSerialBuggyDriver(char const* portName)
{
	if (sys.g_systemInfo.m_osKind != sys.OsKind.Windows)
		return null;

	char const* linkName =
		portName[0] != '\\' ? (char const*) $"\\??\\$portName" :
		strncmp(portName, "\\\\.\\", 4) == 0 ? (char const*) $"\\??\\$(portName + 4)" :
		portName;

	char const* deviceName = try io.getSymbolicLinkTarget(linkName);
	printf("link name: %s, device name: %s\n", linkName, deviceName);

	// when we can't get the device name, usually it's because of no port

	if (!deviceName)
		return null;

	return
		stristr(deviceName, "prolific") ? "Prolific" : // prolific can't cancel IRP_MJ_READs
		// ... add more checks later
		null;
}

void setConservativeSerialSettings(io.Serial* serial)
{
	io.SerialOptions options = serial.m_options;
	options &= io.SerialOptions.WinReadWaitFirstChar;
	options |= io.SerialOptions.WinReadCheckComstat;

	serial.m_options = options;
	serial.m_readInterval = 0;
	serial.m_readParallelism = 1;
}

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

//! @}

} // namespace io
