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
|
||||
#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
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Add table
Reference in a new issue