aboutsummaryrefslogtreecommitdiff
path: root/src/modules/serial_echo/serial_config.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/serial_echo/serial_config.hpp')
-rw-r--r--src/modules/serial_echo/serial_config.hpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/modules/serial_echo/serial_config.hpp b/src/modules/serial_echo/serial_config.hpp
new file mode 100644
index 000000000..523ea938c
--- /dev/null
+++ b/src/modules/serial_echo/serial_config.hpp
@@ -0,0 +1,37 @@
+#pragma once
+
+#include <termios.h>
+
+void
+termios_raw_mode(struct termios & mode) {
+ mode.c_iflag = 0;
+ mode.c_oflag = 0;
+ mode.c_lflag = 0;
+
+ mode.c_cflag &= ~CSIZE;
+ mode.c_cflag |= CS8;
+ mode.c_cflag &= ~CSTOPB;
+ mode.c_cflag &= ~(PARENB | PARODD);
+ mode.c_cflag |= CREAD;
+ mode.c_cflag |= CLOCAL;
+}
+
+bool
+serial_set_raw(int fd) {
+ struct termios mode;
+ bool ok = tcgetattr(fd, &mode) == 0;
+ if (ok)
+ {
+ termios_raw_mode(mode);
+ ok = tcsetattr(fd, TCSANOW, &mode) == 0;
+ }
+ return ok;
+}
+
+bool
+serial_set_speed(int fd, speed_t speed) {
+ struct termios mode;
+ return tcgetattr(fd, &mode) == 0
+ and cfsetspeed(&mode, speed) == 0
+ and tcsetattr(fd, TCSANOW, &mode) == 0;
+}