diff --git a/com-handle.cpp b/com-handle.cpp index a255594..a46e857 100644 --- a/com-handle.cpp +++ b/com-handle.cpp @@ -1,9 +1,68 @@ #include #include + + #include + #include + #include + +bool ComHandle::setLowLatency() +{ + auto handle = serialPort->handle(); + + if(!handle) { + qWarning() << "Failed to get serial handle from OS."; + return false; + } + + // Our first attempt is with termios2 + struct termios2 { + tcflag_t c_iflag; /* input mode flags */ + tcflag_t c_oflag; /* output mode flags */ + tcflag_t c_cflag; /* control mode flags */ + tcflag_t c_lflag; /* local mode flags */ + cc_t c_line; /* line discipline */ + cc_t c_cc[19]; /* control characters */ + speed_t c_ispeed; /* input speed */ + speed_t c_ospeed; /* output speed */ + } tio2; + if (::ioctl(handle, TCGETS2, &tio2) != -1) { + // If it's already in low latency, no further configuration is necessary + if (!(tio2.c_cflag & ASYNC_LOW_LATENCY)) { + tio2.c_cflag |= ASYNC_LOW_LATENCY; + ::ioctl(handle, TCSETS2, &tio2); + } else { + qDebug() << "Low latency mode is already in termios2."; + } + } else { + qWarning() << "Failed to get termios2 struct from system."; + } + + // Check again same configuration with serial_struct + serial_struct serial; + ::memset(&serial, 0, sizeof(serial)); + if (::ioctl(handle, TIOCGSERIAL, &serial) == -1) { + qWarning() << "Failed to get serial_struct from system."; + return false; + } + + // If it's already in low latency, no further configuration is necessary + if (serial.flags & ASYNC_LOW_LATENCY) { + qDebug() << "Low latency mode is already in serial_struct."; + return true; + } + + // It's not possible to check for errors since the driver may not support it + serial.flags |= ASYNC_LOW_LATENCY; + ::ioctl(handle, TIOCSSERIAL, &serial); + + //light at the end of the tunnel + return true; +} ComHandle::ComHandle(QSerialPortInfo p) { serialPort = new QSerialPort(p); + setLowLatency(); } bool ComHandle::open() diff --git a/com-handle.h b/com-handle.h index 4806d49..2303a31 100644 --- a/com-handle.h +++ b/com-handle.h @@ -14,6 +14,7 @@ class ComHandle : public QObject void close(); void write(uint8_t* data, uint16_t length); + bool setLowLatency(); QSerialPort* serialPort;