Skip to content

Commit

Permalink
fix: Linux low latency allow seting without changing low latency mode (
Browse files Browse the repository at this point in the history
…#2241)

* only set lowLatency if defined
* fix set and get methods for low_latency
* clarify error messages

Proposed fix to address #2240
  • Loading branch information
GazHank authored May 26, 2021
1 parent 567b6d8 commit fb53b99
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
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) {

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

if (ioctl(fd, TIOCSSERIAL, &ss) < 0) {
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__)
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

0 comments on commit fb53b99

Please sign in to comment.