From 987b785de479460c4699f59f949e8af31090429b Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 23 May 2008 20:36:31 +0000 Subject: [PATCH] Make the public API return the size of the downloaded data. And renamed a few variables to make the code more consistent with code elsewhere in the library. --- uwatec_smart.c | 30 +++++++++++++++--------------- uwatec_smart_test.c | 4 ++-- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/uwatec_smart.c b/uwatec_smart.c index 7bba158..76507b8 100644 --- a/uwatec_smart.c +++ b/uwatec_smart.c @@ -165,7 +165,7 @@ uwatec_smart_transfer (smart *device, const unsigned char command[], unsigned in int -uwatec_smart_read (smart *device, unsigned char data[], unsigned int msize) +uwatec_smart_read (smart *device, unsigned char data[], unsigned int size) { if (device == NULL) return UWATEC_ERROR; @@ -265,14 +265,14 @@ uwatec_smart_read (smart *device, unsigned char data[], unsigned int msize) if (rc != UWATEC_SUCCESS) return rc; - unsigned int size = answer[0] + (answer[1] << 8) + + unsigned int length = answer[0] + (answer[1] << 8) + (answer[2] << 16) + (answer[3] << 24); - message ("handshake: size=%u\n", size); + message ("handshake: length=%u\n", length); - if (size == 0) - return UWATEC_SUCCESS; + if (length == 0) + return 0; - unsigned char *package = malloc (size * sizeof (unsigned char)); + unsigned char *package = malloc (length * sizeof (unsigned char)); if (package == NULL) { WARNING ("Memory allocation error."); return UWATEC_ERROR_MEMORY; @@ -296,15 +296,15 @@ uwatec_smart_read (smart *device, unsigned char data[], unsigned int msize) return rc; } - unsigned int length = answer[0] + (answer[1] << 8) + + unsigned int total = answer[0] + (answer[1] << 8) + (answer[2] << 16) + (answer[3] << 24); - message ("handshake: size=%u\n", length); + message ("handshake: total=%u\n", total); - assert (length == size + 4); + assert (total == length + 4); unsigned int nbytes = 0; - while (nbytes < size) { - unsigned int len = size - nbytes; + while (nbytes < length) { + unsigned int len = length - nbytes; if (len > 32) len = 32; rc = irda_socket_read (device->socket, package + nbytes, len); @@ -317,16 +317,16 @@ uwatec_smart_read (smart *device, unsigned char data[], unsigned int msize) message ("len=%u, rc=%i, nbytes=%u\n", len, rc, nbytes); } - if (size <= msize) { - memcpy (data, package, size); + if (length <= size) { + memcpy (data, package, length); } else { message ("Insufficient buffer space available.\n"); - memcpy (data, package, msize); + memcpy (data, package, size); } free (package); - return UWATEC_SUCCESS; + return length; } diff --git a/uwatec_smart_test.c b/uwatec_smart_test.c index c3ba9a3..3af3b99 100644 --- a/uwatec_smart_test.c +++ b/uwatec_smart_test.c @@ -31,7 +31,7 @@ int test_dump_memory (const char* filename) message ("uwatec_smart_read\n"); rc = uwatec_smart_read (device, data, size); - if (rc != UWATEC_SUCCESS) { + if (rc < 0) { WARNING ("Cannot read data."); uwatec_smart_close (device); free (data); @@ -41,7 +41,7 @@ int test_dump_memory (const char* filename) message ("Dumping data\n"); FILE* fp = fopen (filename, "wb"); if (fp != NULL) { - fwrite (data, sizeof (unsigned char), size, fp); + fwrite (data, sizeof (unsigned char), rc, fp); fclose (fp); }