diff --git a/include/tabby/printer.h b/include/tabby/printer.h index e88d687..8c754f6 100644 --- a/include/tabby/printer.h +++ b/include/tabby/printer.h @@ -32,10 +32,19 @@ typedef struct _tabby_printer_packet { } tabby_printer_packet; /* - * Methods for communicating with Game Boy as a printer + * For receiving printer data packets from a Game Boy Camera */ -int tabby_printer_packet_recv(int fd, tabby_printer_packet *header, +int tabby_printer_packet_recv(int fd, + tabby_printer_packet *header, void *body, uint16_t *checksum); +/* + * For sending printer data packets to a Game Boy Printer + */ +int tabby_printer_packet_send(int fd, + tabby_printer_packet *header, + void *body, + uint16_t *response); + #endif /* _TABBY_PRINTER_H */ diff --git a/src/printer.c b/src/printer.c index 55fdc45..a1bdd6d 100644 --- a/src/printer.c +++ b/src/printer.c @@ -24,7 +24,8 @@ static uint16_t checksum(tabby_printer_packet *header, void *body) { return checksum; } -int tabby_printer_packet_recv(int fd, tabby_printer_packet *header, +int tabby_printer_packet_recv(int fd, + tabby_printer_packet *header, void *body, uint16_t *sum) { ssize_t len; @@ -89,3 +90,31 @@ int tabby_printer_packet_recv(int fd, tabby_printer_packet *header, error_io: return -errno; } + +int tabby_printer_packet_send(int fd, + tabby_printer_packet *header, + void *body, + uint16_t *response) { + uint16_t sum = checksum(header, body); + + if (write(fd, header, sizeof(*header)) < 0) { + goto error_io; + } + + 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; + } + + return 0; + +error_io: + return -1; +}