From ae8e89eed0f35fc2b0d8668ac5d8743435708619 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 10 Nov 2015 11:34:33 -0600 Subject: [PATCH] examples/media: Need to update position before seeking; handle the too-many-error case more gracefully --- examples/media/media_main.c | 46 +++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/examples/media/media_main.c b/examples/media/media_main.c index 812684320..c8559c5a7 100644 --- a/examples/media/media_main.c +++ b/examples/media/media_main.c @@ -155,7 +155,7 @@ int media_main(int argc, char *argv[]) txbuffer = (FAR uint8_t *)malloc((size_t)info.blocksize); if (txbuffer == NULL) { - fprintf(stderr, "ERROR: failed to allocate TX I/O buffer of size %ul\n", + fprintf(stderr, "ERROR: failed to allocate TX I/O buffer of size %lu\n", (unsigned long)info.blocksize); close(fd); return 1; @@ -164,7 +164,7 @@ int media_main(int argc, char *argv[]) rxbuffer = (FAR uint8_t *)malloc((size_t)info.blocksize); if (rxbuffer == NULL) { - fprintf(stderr, "ERROR: failed to allocate IRX /O buffer of size %ul\n", + fprintf(stderr, "ERROR: failed to allocate IRX /O buffer of size %lu\n", (unsigned long)info.blocksize); free(txbuffer); close(fd); @@ -275,23 +275,27 @@ int media_main(int argc, char *argv[]) } else { - for (i = 0; i < info.blocksize; i++) + for (i = 0; i < info.blocksize && nerrors <= 100; i++) { if (txbuffer[i] != rxbuffer[i]) { fprintf(stderr, - "ERROR: block=%ul offset=%lu. Unexpected value: %02x vs. %02x\n", + "ERROR: block=%lu offset=%lu. Unexpected value: %02x vs. %02x\n", blockno, i, rxbuffer[i], txbuffer[i]); - if (++nerrors > 100) - { - fprintf(stderr, "ERROR: Too many errors\n"); - fprintf(stderr, "ERROR: Aborting at block: %lu\n", blockno); - info.nblocks = blockno; - break; - } + nerrors++; } } + + if (nerrors > 100) + { + fprintf(stderr, "ERROR: Too many errors\n"); + fprintf(stderr, "ERROR: Aborting at block: %lu\n", blockno); + info.nblocks = blockno; + break; + } } + + pos += info.blocksize; } /* Set the number of blocks if it was unknown before */ @@ -354,22 +358,24 @@ int media_main(int argc, char *argv[]) } else { - for (i = 0; i < info.blocksize; i++) + for (i = 0; i < info.blocksize && nerrors <= 100; i++) { if (txbuffer[i] != rxbuffer[i]) { fprintf(stderr, - "ERROR: block=%ul offset=%lu. Unexpected value: %02x vs. %02x\n", + "ERROR: block=%lu offset=%lu. Unexpected value: %02x vs. %02x\n", blockno, i, rxbuffer[i], txbuffer[i]); - - if (++nerrors > 100) - { - fprintf(stderr, "ERROR: Too many errors\n"); - fprintf(stderr, "ERROR: Aborting at block: %lu\n", blockno); - break; - } + nerrors++; } } + + if (nerrors > 100) + { + fprintf(stderr, "ERROR: Too many errors\n"); + fprintf(stderr, "ERROR: Aborting at block: %lu\n", blockno); + info.nblocks = blockno; + break; + } } }