Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux low latency fix (allow set without changing low latency mode) #2241

Merged
merged 5 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions packages/bindings/src/serialport_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate) {
t.c_cflag |= BOTHER;
t.c_ospeed = t.c_ispeed = baudrate;

if (ioctl(fd, TCSETS2, &t)) {
if (ioctl(fd, TCSETS2, &t) < 0) {
return -2;
}

Expand All @@ -44,16 +44,30 @@ int linuxSetLowLatencyMode(const int fd, const bool enable) {
return -1;
}

if (enable) {
ss.flags |= ASYNC_LOW_LATENCY;
} else {
ss.flags &= ~ASYNC_LOW_LATENCY;
if (ss.flags & ASYNC_LOW_LATENCY != enable) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a nitpick it's better to return early than it is to nest

if (enable) {
ss.flags |= ASYNC_LOW_LATENCY;
} else {
ss.flags &= ~ASYNC_LOW_LATENCY;
}

if (ioctl(fd, TIOCSSERIAL, &ss) < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another nitpick ioctl will always return -1 on error never anything else

return -2;
}
}
return 0;
}

if (ioctl(fd, TIOCSSERIAL, &ss)) {
return -2;
int linuxGetLowLatencyMode(const int fd, bool* const enabled) {
struct serial_struct ss;

if (ioctl(fd, TIOCGSERIAL, &ss)) {
return -1;
}

*enabled = ss.flags & ASYNC_LOW_LATENCY;

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions packages/bindings/src/serialport_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate);
int linuxGetSystemBaudRate(const int fd, int* const outbaud);
int linuxSetLowLatencyMode(const int fd, const bool enable);
int linuxGetLowLatencyMode(const int fd, bool* const enabled);

#endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_LINUX_H_

41 changes: 21 additions & 20 deletions packages/bindings/src/serialport_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,6 @@ void EIO_Set(uv_work_t* req) {
bits |= TIOCM_DSR;
}

#if defined(__linux__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good move

int err = linuxSetLowLatencyMode(data->fd, data->lowLatency);
if (err == -1) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, cannot get", strerror(errno));
return;
} else if(err == -2) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, cannot set", strerror(errno));
return;
}
#endif

int result = 0;
if (data->brk) {
result = ioctl(data->fd, TIOCSBRK, NULL);
Expand All @@ -359,6 +348,17 @@ void EIO_Set(uv_work_t* req) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, cannot set", strerror(errno));
return;
}

#if defined(__linux__)
int err = linuxSetLowLatencyMode(data->fd, data->lowLatency);
if (err == -1) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, cannot get low latency", strerror(errno));
return;
} else if(err == -2) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, cannot set low latency", strerror(errno));
return;
}
#endif
}

void EIO_Get(uv_work_t* req) {
Expand All @@ -370,19 +370,20 @@ void EIO_Get(uv_work_t* req) {
return;
}

data->lowLatency = false;
data->cts = bits & TIOCM_CTS;
data->dsr = bits & TIOCM_DSR;
data->dcd = bits & TIOCM_CD;

#if defined(__linux__) && defined(ASYNC_LOW_LATENCY)
int latency_bits;
if (-1 == ioctl(data->fd, TIOCGSERIAL, &latency_bits)) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, cannot get", strerror(errno));
bool lowlatency = false;
if (-1 == linuxGetLowLatencyMode(data->fd, &lowlatency)) {
snprintf(data->errorString, sizeof(data->errorString), "Error: %s, cannot get low latency", strerror(errno));
return;
}
data->lowLatency = latency_bits & ASYNC_LOW_LATENCY;
data->lowLatency = lowlatency;
#else
data->lowLatency = false;
#endif

data->cts = bits & TIOCM_CTS;
data->dsr = bits & TIOCM_DSR;
data->dcd = bits & TIOCM_CD;
}

void EIO_GetBaudRate(uv_work_t* req) {
Expand Down