Implement retry mechanism
This commit is contained in:
parent
d670c605a1
commit
70f1fc349e
2 changed files with 36 additions and 15 deletions
|
@ -1,6 +1,9 @@
|
||||||
#ifndef _TABBY_PRINTER_H
|
#ifndef _TABBY_PRINTER_H
|
||||||
#define _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_MAX_PACKET_SIZE 640
|
||||||
|
|
||||||
#define TABBY_PRINTER_BAND_SIZE 640
|
#define TABBY_PRINTER_BAND_SIZE 640
|
||||||
|
|
|
@ -107,11 +107,31 @@ error_io:
|
||||||
return -errno;
|
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,
|
int tabby_printer_packet_send(int fd,
|
||||||
tabby_printer_packet *header,
|
tabby_printer_packet *header,
|
||||||
void *body,
|
void *body,
|
||||||
tabby_printer_response *response) {
|
tabby_printer_response *response) {
|
||||||
uint16_t sum = checksum(header, body);
|
uint16_t sum = checksum(header, body);
|
||||||
|
int tries = TABBY_PRINTER_RETRY_COUNT;
|
||||||
|
|
||||||
|
do {
|
||||||
|
if (tries-- == 0) {
|
||||||
|
errno = EIO;
|
||||||
|
|
||||||
|
goto error_io;
|
||||||
|
}
|
||||||
|
|
||||||
if (write(fd, header, sizeof(*header)) < 0) {
|
if (write(fd, header, sizeof(*header)) < 0) {
|
||||||
goto error_io;
|
goto error_io;
|
||||||
|
@ -130,6 +150,7 @@ int tabby_printer_packet_send(int fd,
|
||||||
if (read(fd, response, sizeof(*response)) < 0) {
|
if (read(fd, response, sizeof(*response)) < 0) {
|
||||||
goto error_io;
|
goto error_io;
|
||||||
}
|
}
|
||||||
|
} while (resend(response));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -210,9 +231,6 @@ int tabby_printer_send_sheet(int fd, void *data,
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += TABBY_PRINTER_BAND_SIZE;
|
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);
|
init_header(&header, TABBY_PRINTER_PACKET_DATA, 0);
|
||||||
|
|
Loading…
Add table
Reference in a new issue