rs

FTDI serial communication program
git clone git://git.2f30.org/rs
Log | Files | Refs | README | LICENSE

commit a2c47b1274f5eea99f53d1ac71819636ddac12e3
parent 60a899185dad84b492a9ad0ba58b6a8de0025f92
Author: sin <sin@2f30.org>
Date:   Sat, 16 Nov 2013 18:11:35 +0000

Replace fprintf() calls with enprintf()

Some more error checking too.

Diffstat:
Mrs.c | 51++++++++++++++++++++++++++-------------------------
1 file changed, 26 insertions(+), 25 deletions(-)

diff --git a/rs.c b/rs.c @@ -70,9 +70,8 @@ loop(void) n = ftdi_read_data(&ftdictx, buf, sizeof(buf)); if (n < 0) { ttyrestore(); - fprintf(stderr, "ftdi_read_data: read error: %s\n", - ftdi_get_error_string(&ftdictx)); - break; + enprintf(EXIT_FAILURE, "ftdi_read_data: read error: %s\n", + ftdi_get_error_string(&ftdictx)); } write(STDOUT_FILENO, buf, n); } @@ -92,11 +91,10 @@ listdevs(void) ret = ftdi_usb_get_strings(&ftdictx, iter->dev, manufacturer, sizeof(manufacturer), description, sizeof(description), NULL, 0); - if (ret < 0) { - fprintf(stderr, "ftdi_usb_get_strings: %s\n", - ftdi_get_error_string(&ftdictx)); - exit(EXIT_FAILURE); - } + if (ret < 0) + enprintf(EXIT_FAILURE, "ftdi_usb_get_strings: %s\n", + ftdi_get_error_string(&ftdictx)); + printf("device %d: manufacturer: %s, description: %s, vendor = %04x, product = %04x\n", i, manufacturer, description, iter->dev->descriptor.idVendor, iter->dev->descriptor.idProduct); @@ -154,15 +152,18 @@ main(int argc, char *argv[]) if (!isatty(STDIN_FILENO)) eprintf("isatty: stdin:"); - ftdi_init(&ftdictx); - ftdi_set_interface(&ftdictx, INTERFACE_ANY); + ret = ftdi_init(&ftdictx); + if (ret < 0) + enprintf(EXIT_FAILURE, "failed to initialize ftdi context\n"); + ret = ftdi_set_interface(&ftdictx, INTERFACE_ANY); + if (ret < 0) + enprintf(EXIT_FAILURE, "ftdi_set_interface: %s\n", + ftdi_get_error_string(&ftdictx)); ret = ftdi_usb_find_all(&ftdictx, &devlist, vendor, product); - if (ret < 0) { - fprintf(stderr, "ftdi_usb_find_all: %s\n", - ftdi_get_error_string(&ftdictx)); - exit(EXIT_FAILURE); - } + if (ret < 0) + enprintf(EXIT_FAILURE, "ftdi_usb_find_all: %s\n", + ftdi_get_error_string(&ftdictx)); if (lflag) { listdevs(); @@ -177,18 +178,18 @@ main(int argc, char *argv[]) break; } } - if (!iter) { - fprintf(stderr, "Can't find device %d\n", dev); - exit(EXIT_FAILURE); - } + if (!iter) + enprintf(EXIT_FAILURE, "Can't find device %d\n", dev); } - if (ftdi_usb_open(&ftdictx, vendor, product) < 0) { - fprintf(stderr, "ftdi_usb_open: %04x:%04x: %s\n", - vendor, product, ftdi_get_error_string(&ftdictx)); - exit(EXIT_FAILURE); - } - ftdi_set_baudrate(&ftdictx, baudrate); + ret = ftdi_usb_open(&ftdictx, vendor, product); + if (ret < 0) + enprintf(EXIT_FAILURE, "ftdi_usb_open: %04x:%04x: %s\n", + vendor, product, ftdi_get_error_string(&ftdictx)); + ret = ftdi_set_baudrate(&ftdictx, baudrate); + if (ret < 0) + enprintf(EXIT_FAILURE, "ftdi_set_baudrate: %d: %s\n", + baudrate, ftdi_get_error_string(&ftdictx)); printf("Break key is set to ^\\\n");