ao-tools/lib: Deal with binary USB data in debugging output
[fw/altos] / ao-tools / lib / cc-usb.c
index 9f07e6624d2c849f7469c07cbc80dc018648f383..485583f98b3897c1816547ffc4b7ae95ee32a6b1 100644 (file)
@@ -140,7 +140,10 @@ cc_usb_dbg(int indent, uint8_t *bytes, int len)
                case '\n':
                        eol = 1;
                default:
-                       ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
+                       if (c < ' ' || c > '~')
+                               ccdbg_debug(CC_DEBUG_BITBANG, "\\%02x", c);
+                       else
+                               ccdbg_debug(CC_DEBUG_BITBANG, "%c", c);
                }
        }
 }
@@ -190,6 +193,7 @@ _cc_usb_sync(struct cc_usb *cc, int wait_for_input)
                        ret = read(cc->fd, cc->in_buf + cc->in_count,
                                   CC_IN_BUF - cc->in_count);
                        if (ret > 0) {
+                               int i;
                                cc_usb_dbg(24, cc->in_buf + cc->in_count, ret);
                                cc->in_count += ret;
                                if (cc->hex_count)
@@ -254,10 +258,10 @@ cc_usb_printf(struct cc_usb *cc, char *format, ...)
 }
 
 int
-cc_usb_getchar(struct cc_usb *cc)
+cc_usb_getchar_timeout(struct cc_usb *cc, int timeout)
 {
        while (cc->in_pos == cc->in_count) {
-               if (_cc_usb_sync(cc, 5000) < 0) {
+               if (_cc_usb_sync(cc, timeout) < 0) {
                        fprintf(stderr, "USB link timeout\n");
                        exit(1);
                }
@@ -265,6 +269,12 @@ cc_usb_getchar(struct cc_usb *cc)
        return cc->in_buf[cc->in_pos++];
 }
 
+int
+cc_usb_getchar(struct cc_usb *cc)
+{
+       return cc_usb_getchar_timeout(cc, 5000);
+}
+
 void
 cc_usb_getline(struct cc_usb *cc, char *line, int max)
 {
@@ -420,6 +430,8 @@ cc_usb_open(char *tty)
        tcgetattr(cc->fd, &termios);
        save_termios = termios;
        cfmakeraw(&termios);
+       cfsetospeed(&termios, B9600);
+       cfsetispeed(&termios, B9600);
        tcsetattr(cc->fd, TCSAFLUSH, &termios);
        cc_usb_printf(cc, "\nE 0\nm 0\n");
        do {