diff --git a/include/tabby/printer.h b/include/tabby/printer.h index 8783f0b..969eb7f 100644 --- a/include/tabby/printer.h +++ b/include/tabby/printer.h @@ -1,6 +1,9 @@ #ifndef _TABBY_PRINTER_H #define _TABBY_PRINTER_H +#define TABBY_PRINTER_DEVICE_ID 0x81 +#define TABBY_PRINTER_RETRY_COUNT 5 + #define TABBY_PRINTER_MAX_PACKET_SIZE 640 #define TABBY_PRINTER_BAND_SIZE 640 diff --git a/src/printer.c b/src/printer.c index 81ec10a..b204aaf 100644 --- a/src/printer.c +++ b/src/printer.c @@ -107,29 +107,50 @@ error_io: return -errno; } +static inline int resend(tabby_printer_response *response) { + if (response->device != TABBY_PRINTER_DEVICE_ID) { + return 1; + } + + if (response->status & (TABBY_PRINTER_ER0 | TABBY_PRINTER_SUM)) { + return 1; + } + + return 0; +} + int tabby_printer_packet_send(int fd, tabby_printer_packet *header, void *body, tabby_printer_response *response) { uint16_t sum = checksum(header, body); + int tries = TABBY_PRINTER_RETRY_COUNT; - if (write(fd, header, sizeof(*header)) < 0) { - goto error_io; - } + do { + if (tries-- == 0) { + errno = EIO; - if (header->size) { - if (write(fd, body, header->size) < 0) { goto error_io; } - } - if (write(fd, &sum, sizeof(sum)) < 0) { - goto error_io; - } + if (write(fd, header, sizeof(*header)) < 0) { + goto error_io; + } - if (read(fd, response, sizeof(*response)) < 0) { - goto error_io; - } + if (header->size) { + if (write(fd, body, header->size) < 0) { + goto error_io; + } + } + + if (write(fd, &sum, sizeof(sum)) < 0) { + goto error_io; + } + + if (read(fd, response, sizeof(*response)) < 0) { + goto error_io; + } + } while (resend(response)); return 0; @@ -210,9 +231,6 @@ int tabby_printer_send_sheet(int fd, void *data, } offset += TABBY_PRINTER_BAND_SIZE; - - printf("Sent band, got status %02x%02x\n", - current.device, current.status); } init_header(&header, TABBY_PRINTER_PACKET_DATA, 0);