Quellcode durchsuchen

fix(thermal): prevent hardware TTY empty read(0) from triggering false EOF disconnect

XJKJ STL vor 2 Wochen
Ursprung
Commit
8893c4abc9
3 geänderte Dateien mit 39 neuen und 4 gelöschten Zeilen
  1. 31 3
      native/stl_accel/c_bridge.c
  2. 1 0
      native/stl_accel/src/lib.rs
  3. 7 1
      native/stl_accel/src/thermal_mcu.rs

+ 31 - 3
native/stl_accel/c_bridge.c

@@ -543,12 +543,16 @@ int stl_open_tty_115200_raw(const char *path) {
     tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
     tio.c_cflag |= (CS8 | CLOCAL | CREAD);
     tio.c_iflag &= ~(IXON | IXOFF | IXANY);
-    tio.c_cc[VMIN]  = 0;
+    tio.c_cc[VMIN]  = 1;
     tio.c_cc[VTIME] = 0;
     if (tcsetattr(fd, TCSANOW, &tio) != 0) {
         close(fd);
         return -1;
     }
+    int fl = fcntl(fd, F_GETFL, 0);
+    if (fl >= 0) {
+        (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK);
+    }
     return fd;
 }
 
@@ -578,12 +582,16 @@ int c_open_tty_baud_raw(const char *path, int baud) {
     tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
     tio.c_cflag |= (CS8 | CLOCAL | CREAD);
     tio.c_iflag &= ~(IXON | IXOFF | IXANY);
-    tio.c_cc[VMIN]  = 0;
+    tio.c_cc[VMIN]  = 1;
     tio.c_cc[VTIME] = 0;
     if (tcsetattr(fd, TCSANOW, &tio) != 0) {
         close(fd);
         return -1;
     }
+    int fl = fcntl(fd, F_GETFL, 0);
+    if (fl >= 0) {
+        (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK);
+    }
     return fd;
 }
 
@@ -611,12 +619,16 @@ int c_open_mcu_tty_baud_raw(const char *path, int baud) {
     tio.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
     tio.c_cflag |= (CS8 | CLOCAL | CREAD);
     tio.c_iflag &= ~(IXON | IXOFF | IXANY);
-    tio.c_cc[VMIN]  = 0;
+    tio.c_cc[VMIN]  = 1;
     tio.c_cc[VTIME] = 0;
     if (tcsetattr(fd, TCSANOW, &tio) != 0) {
         close(fd);
         return -1;
     }
+    int fl = fcntl(fd, F_GETFL, 0);
+    if (fl >= 0) {
+        (void)fcntl(fd, F_SETFL, fl | O_NONBLOCK);
+    }
     return fd;
 }
 
@@ -818,6 +830,22 @@ ssize_t c_pty_read_nonblock(int master_fd, uint8_t *buf, size_t cap) {
     return -2; /* EIO / closed */
 }
 
+ssize_t c_mcu_tty_read_nonblock(int fd, uint8_t *buf, size_t cap, int is_serial) {
+    if (fd < 0 || !buf || cap == 0) return -1;
+    ssize_t n = read(fd, buf, cap);
+    if (n > 0) return n;
+    if (n == 0) {
+        if (!is_serial) return -2; /* TCP EOF */
+        struct pollfd pfd = { .fd = fd, .events = POLLIN, .revents = 0 };
+        if (poll(&pfd, 1, 0) > 0 && (pfd.revents & (POLLHUP | POLLERR | POLLNVAL))) {
+            return -2; /* USB serial unplugged / hangup */
+        }
+        return 0; /* TTY VMIN=0 empty buffer, not EOF */
+    }
+    if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return 0;
+    return -2; /* EIO / ENODEV / closed */
+}
+
 ssize_t c_pty_write_bytes(int master_fd, const uint8_t *buf, size_t len) {
     if (master_fd < 0 || !buf || len == 0) return 0;
     ssize_t n = write(master_fd, buf, len);

+ 1 - 0
native/stl_accel/src/lib.rs

@@ -271,6 +271,7 @@ extern "C" {
         out_child_pid: *mut i32,
     ) -> i32;
     pub fn c_pty_read_nonblock(master_fd: i32, buf: *mut u8, cap: usize) -> isize;
+    pub fn c_mcu_tty_read_nonblock(fd: i32, buf: *mut u8, cap: usize, is_serial: i32) -> isize;
     pub fn c_pty_write_bytes(master_fd: i32, buf: *const u8, len: usize) -> isize;
     pub fn c_pty_is_alive(child_pid: i32) -> i32;
     pub fn c_pty_close(master_fd: i32, child_pid: i32);

+ 7 - 1
native/stl_accel/src/thermal_mcu.rs

@@ -3548,9 +3548,15 @@ impl ThermalMcuHub {
 
                     // 3. Drain non-blocking physical serial / TCP socket
                     if rt.fd >= 0 {
+                        let is_serial = if rt.transport_mode == "serial" { 1 } else { 0 };
                         loop {
                             let n = unsafe {
-                                lib::c_pty_read_nonblock(rt.fd, rx_buf.as_mut_ptr(), rx_buf.len())
+                                lib::c_mcu_tty_read_nonblock(
+                                    rt.fd,
+                                    rx_buf.as_mut_ptr(),
+                                    rx_buf.len(),
+                                    is_serial,
+                                )
                             };
                             if n > 0 {
                                 let items = rt.parser.feed(&rx_buf[..n as usize], now);