Added low latency mode for linux (#2102)

* Added low latency mode for linux

Co-authored-by: 0xJeremy <jeremy.kanovsky@tufts.edu>
This commit is contained in:
Jeremy Kanovsky 2021-02-21 17:55:07 -05:00 • committed by GitHub
parent d13514b10b
commit 40ca98a867
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 0 deletions

View file

@ -155,6 +155,7 @@ The in progress writes must error when the port is closed with an error object t
* @param {Boolean} [options.dsr=false] flag for dsr
* @param {Boolean} [options.dtr=true] flag for dtr
* @param {Boolean} [options.rts=true] flag for rts
* @param {Boolean} [options.lowLatency=false] flag for lowLatency mode on Linux
* @returns {Promise} Resolves once the port's flags are set.
* @rejects {TypeError} When given invalid arguments, a `TypeError` is rejected.
*/

View file

@ -29,6 +29,7 @@ const defaultSetFlags = Object.freeze({
dtr: true,
dts: false,
rts: true,
lowLatency: false,
})
const listFields = ['path', 'manufacturer', 'serialNumber', 'pnpId', 'locationId', 'vendorId', 'productId']

View file

@ -264,6 +264,7 @@ NAN_METHOD(Set) {
baton->cts = getBoolFromObject(options, "cts");
baton->dtr = getBoolFromObject(options, "dtr");
baton->dsr = getBoolFromObject(options, "dsr");
baton->lowLatency = getBoolFromObject(options, "lowLatency");
uv_work_t* req = new uv_work_t();
req->data = baton;
@ -307,6 +308,7 @@ NAN_METHOD(Get) {
baton->cts = false;
baton->dsr = false;
baton->dcd = false;
baton->lowLatency = false;
baton->callback.Reset(info[1].As<v8::Function>());
uv_work_t* req = new uv_work_t();
@ -329,6 +331,7 @@ void EIO_AfterGet(uv_work_t* req) {
Nan::Set(results, Nan::New<v8::String>("cts").ToLocalChecked(), Nan::New<v8::Boolean>(data->cts));
Nan::Set(results, Nan::New<v8::String>("dsr").ToLocalChecked(), Nan::New<v8::Boolean>(data->dsr));
Nan::Set(results, Nan::New<v8::String>("dcd").ToLocalChecked(), Nan::New<v8::Boolean>(data->dcd));
Nan::Set(results, Nan::New<v8::String>("lowLatency").ToLocalChecked(), Nan::New<v8::Boolean>(data->lowLatency));
argv[0] = Nan::Null();
argv[1] = results;

View file

@ -105,6 +105,7 @@ struct SetBaton : public Nan::AsyncResource {
bool dtr = false;
bool dsr = false;
bool brk = false;
bool lowLatency = false;
};
struct GetBaton : public Nan::AsyncResource {
@ -115,6 +116,7 @@ struct GetBaton : public Nan::AsyncResource {
bool cts = false;
bool dsr = false;
bool dcd = false;
bool lowLatency = false;
};
struct GetBaudRateBaton : public Nan::AsyncResource {

View file

@ -3,6 +3,7 @@
#include <sys/ioctl.h>
#include <asm/ioctls.h>
#include <asm/termbits.h>
#include <linux/serial.h>
// Uses the termios2 interface to set nonstandard baud rates
int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate) {
@ -36,4 +37,24 @@ int linuxGetSystemBaudRate(const int fd, int* const outbaud) {
return 0;
}
int linuxSetLowLatencyMode(const int fd, const bool enable) {
struct serial_struct ss;
if (ioctl(fd, TIOCGSERIAL, &ss)) {
return -1;
}
if (enable) {
ss.flags |= ASYNC_LOW_LATENCY;
} else {
ss.flags &= ~ASYNC_LOW_LATENCY;
}
if (ioctl(fd, TIOCSSERIAL, &ss)) {
return -2;
}
return 0;
}
#endif

View file

@ -3,6 +3,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);
#endif // PACKAGES_SERIALPORT_SRC_SERIALPORT_LINUX_H_

View file

@ -332,6 +332,17 @@ 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);
@ -359,6 +370,16 @@ void EIO_Get(uv_work_t* req) {
return;
}
data->lowLatency = false;
#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));
return;
}
data->lowLatency = latency_bits & ASYNC_LOW_LATENCY;
#endif
data->cts = bits & TIOCM_CTS;
data->dsr = bits & TIOCM_DSR;
data->dcd = bits & TIOCM_CD;