forked from ROMEO/fsw-ws
69 lines
1.4 KiB
C
69 lines
1.4 KiB
C
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdint.h>
|
|
#include <termios.h>
|
|
#include <unistd.h>
|
|
|
|
// TODO FIXME
|
|
int compare_string_chars(const char *c_string, const char *chars,
|
|
size_t chars_len);
|
|
|
|
int convert_errno(int errno_value) {
|
|
// errno on linux will always be >0
|
|
if (errno <= 0) {
|
|
// something is very wrong
|
|
return -1;
|
|
} else {
|
|
return -errno - 1;
|
|
}
|
|
}
|
|
|
|
// TODO: can we extend errno safely?
|
|
// returns fd if ok, -1 on error 0 if no match
|
|
int serial_open_actual(const char *path, speed_t speed) {
|
|
// open serial
|
|
int fd = open(path, O_RDWR | O_NOCTTY | O_SYNC);
|
|
if (fd < 0) {
|
|
return -1;
|
|
}
|
|
|
|
struct termios termios;
|
|
|
|
// initialize termios struct
|
|
int ret = tcgetattr(fd, &termios);
|
|
if (ret < 0) {
|
|
return -1;
|
|
}
|
|
|
|
// configure for raw input
|
|
cfmakeraw(&termios);
|
|
|
|
// make it non-blocking
|
|
termios.c_cc[VMIN] = 0;
|
|
termios.c_cc[VTIME] = 0;
|
|
|
|
// set speed
|
|
ret = cfsetspeed(&termios, speed);
|
|
if (ret < 0) {
|
|
return -1;
|
|
}
|
|
|
|
ret = tcsetattr(fd, TCSANOW, &termios);
|
|
if (ret < 0) {
|
|
return -1;
|
|
}
|
|
|
|
return fd;
|
|
}
|
|
|
|
|
|
// returns fd if success, -1 on error, -2 if no match
|
|
int serial_open(const char *path, size_t path_len) {
|
|
if (compare_string_chars("ps/uart_mtg", path, path_len) == 1) {
|
|
return serial_open_actual("/dev/ttyUSB0", B921600);
|
|
}
|
|
if (compare_string_chars("debug/uart🚀", path, path_len) == 1) {
|
|
return serial_open_actual("/dev/ttyUSB0", B115200);
|
|
}
|
|
return -2;
|
|
} |