examples/media: Need to update position before seeking; handle the too-many-error case more gracefully

This commit is contained in:
Gregory Nutt 2015-11-10 11:34:33 -06:00
parent a584ef09aa
commit ae8e89eed0

View File

@ -155,7 +155,7 @@ int media_main(int argc, char *argv[])
txbuffer = (FAR uint8_t *)malloc((size_t)info.blocksize); txbuffer = (FAR uint8_t *)malloc((size_t)info.blocksize);
if (txbuffer == NULL) 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); (unsigned long)info.blocksize);
close(fd); close(fd);
return 1; return 1;
@ -164,7 +164,7 @@ int media_main(int argc, char *argv[])
rxbuffer = (FAR uint8_t *)malloc((size_t)info.blocksize); rxbuffer = (FAR uint8_t *)malloc((size_t)info.blocksize);
if (rxbuffer == NULL) 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); (unsigned long)info.blocksize);
free(txbuffer); free(txbuffer);
close(fd); close(fd);
@ -275,23 +275,27 @@ int media_main(int argc, char *argv[])
} }
else else
{ {
for (i = 0; i < info.blocksize; i++) for (i = 0; i < info.blocksize && nerrors <= 100; i++)
{ {
if (txbuffer[i] != rxbuffer[i]) if (txbuffer[i] != rxbuffer[i])
{ {
fprintf(stderr, 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]); blockno, i, rxbuffer[i], txbuffer[i]);
if (++nerrors > 100) nerrors++;
{
fprintf(stderr, "ERROR: Too many errors\n");
fprintf(stderr, "ERROR: Aborting at block: %lu\n", blockno);
info.nblocks = blockno;
break;
}
} }
} }
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 */ /* Set the number of blocks if it was unknown before */
@ -354,22 +358,24 @@ int media_main(int argc, char *argv[])
} }
else else
{ {
for (i = 0; i < info.blocksize; i++) for (i = 0; i < info.blocksize && nerrors <= 100; i++)
{ {
if (txbuffer[i] != rxbuffer[i]) if (txbuffer[i] != rxbuffer[i])
{ {
fprintf(stderr, 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]); blockno, i, rxbuffer[i], txbuffer[i]);
nerrors++;
if (++nerrors > 100)
{
fprintf(stderr, "ERROR: Too many errors\n");
fprintf(stderr, "ERROR: Aborting at block: %lu\n", blockno);
break;
}
} }
} }
if (nerrors > 100)
{
fprintf(stderr, "ERROR: Too many errors\n");
fprintf(stderr, "ERROR: Aborting at block: %lu\n", blockno);
info.nblocks = blockno;
break;
}
} }
} }