First Version of IO
This commit is contained in:
1 parent
556faaba08
commit
3af76b93b2
10 files changed
+809
-61
No files matched your search
@@ -46,7 +46,7 @@ int hw_device_open(const char *path, size_t path_len) {
|
||||
|
||||
int serial_fd = serial_open(path, path_len);
|
||||
|
||||
if (serial_fd != -1) {
|
||||
if (serial_fd >= -1) {
|
||||
return serial_fd;
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ int hw_device_open(const char *path, size_t path_len) {
|
||||
|
||||
for (current_candidate = addr_candidates; current_candidate != NULL;
|
||||
current_candidate = current_candidate->ai_next) {
|
||||
sock = socket(current_candidate->ai_family, current_candidate->ai_socktype,
|
||||
sock = socket(current_candidate->ai_family, current_candidate->ai_socktype | SOCK_NONBLOCK,
|
||||
current_candidate->ai_protocol);
|
||||
if (sock == -1) {
|
||||
continue;
|
||||
|
||||
@@ -18,12 +18,13 @@ int convert_errno(int errno_value) {
|
||||
}
|
||||
}
|
||||
|
||||
// returns fd if ok, converted errno if error
|
||||
// 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 convert_errno(errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct termios termios;
|
||||
@@ -31,7 +32,7 @@ int serial_open_actual(const char *path, speed_t speed) {
|
||||
// initialize termios struct
|
||||
int ret = tcgetattr(fd, &termios);
|
||||
if (ret < 0) {
|
||||
return convert_errno(errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// configure for raw input
|
||||
@@ -44,19 +45,19 @@ int serial_open_actual(const char *path, speed_t speed) {
|
||||
// set speed
|
||||
ret = cfsetspeed(&termios, speed);
|
||||
if (ret < 0) {
|
||||
return convert_errno(errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = tcsetattr(fd, TCSANOW, &termios);
|
||||
if (ret < 0) {
|
||||
return convert_errno(errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
||||
// returns fd if success, -1 if no path match <-1 if error
|
||||
// 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);
|
||||
@@ -64,5 +65,5 @@ int serial_open(const char *path, size_t path_len) {
|
||||
if (compare_string_chars("debug/uart🚀", path, path_len) == 1) {
|
||||
return serial_open_actual("/dev/ttyUSB0", B115200);
|
||||
}
|
||||
return -1;
|
||||
return -2;
|
||||
}
|
||||
Reference in new issue
Block a user