Browse Source

sendbrk: trivial tool to send a BREAK on a serial port

The USBCAS firmware goes into firmware download mode if it receives a
BREAK, so quick tool to send one.
H. Peter Anvin 3 years ago
parent
commit
47b0cf9789
1 changed files with 49 additions and 0 deletions
  1. 49 0
      sendbrk/sendbrk.c

+ 49 - 0
sendbrk/sendbrk.c

@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
+#include <errno.h>
+#include <string.h>
+
+const char *_progname;
+
+static int send_break(const char *port)
+{
+    int err;
+    int fd = open(port, O_RDWR|O_NOCTTY|O_NONBLOCK);
+
+    if (fd < 0)
+	goto fail;
+
+    tcdrain(fd);
+
+    do {
+	err = tcsendbreak(fd, 0);
+    } while (err && errno == EAGAIN);
+
+    if (err)
+	goto fail;
+
+    close(fd);
+    return 0;
+
+fail:
+    fprintf(stderr, "%s: %s: %s\n", _progname, port, strerror(errno));
+    if (fd >= 0)
+	close(fd);
+    return -1;
+}
+
+int main(int argc, char *argv[])
+{
+    _progname = argv[0];
+    
+    if (argc != 2) {
+	fprintf(stderr, "Usage: %s port\n", _progname);
+	exit(1);
+    }
+
+    send_break(argv[1]);
+    return 0;
+}