Stop abusing status codes to return size information to the application.

When necessary, an output parameter is added to provide the size 
information. Status codes are strictly reserved for providing status 
information only.
This commit is contained in:
Jef Driesen 2008-08-28 09:07:09 +00:00
parent d9170ad576
commit b031d8dc2c
20 changed files with 138 additions and 88 deletions

View File

@ -36,8 +36,9 @@ int test_dump_memory (const char* name, const char* filename)
message ("time=%lu (%s)\n", (unsigned long)now, datetime);
message ("device_dump\n");
rc = device_dump (device, data, sizeof (data));
if (rc < 0) {
unsigned int nbytes = 0;
rc = device_dump (device, data, sizeof (data), &nbytes);
if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read memory.");
device_close (device);
return rc;
@ -46,7 +47,7 @@ int test_dump_memory (const char* name, const char* filename)
message ("Dumping data\n");
FILE* fp = fopen (filename, "wb");
if (fp != NULL) {
fwrite (data, sizeof (unsigned char), rc, fp);
fwrite (data, sizeof (unsigned char), nbytes, fp);
fclose (fp);
}

View File

@ -81,8 +81,9 @@ int test_dump_memory_data (const char* name, const char* filename)
message ("time=%lu (%s)\n", (unsigned long)now, datetime);
message ("device_dump\n");
rc = device_dump (device, data, sizeof (data));
if (rc < 0) {
unsigned int nbytes = 0;
rc = device_dump (device, data, sizeof (data), &nbytes);
if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read memory.");
device_close (device);
return rc;
@ -91,7 +92,7 @@ int test_dump_memory_data (const char* name, const char* filename)
message ("Dumping data\n");
FILE* fp = fopen (filename, "wb");
if (fp != NULL) {
fwrite (data, sizeof (unsigned char), rc, fp);
fwrite (data, sizeof (unsigned char), nbytes, fp);
fclose (fp);
}

View File

@ -21,8 +21,9 @@ int test_dump_memory (const char* name, const char* filename)
}
message ("device_dump\n");
rc = device_dump (device, data, sizeof (data));
if (rc < 0) {
unsigned int nbytes = 0;
rc = device_dump (device, data, sizeof (data), &nbytes);
if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read memory.");
device_close (device);
return rc;
@ -31,7 +32,7 @@ int test_dump_memory (const char* name, const char* filename)
message ("Dumping data\n");
FILE* fp = fopen (filename, "wb");
if (fp != NULL) {
fwrite (data, sizeof (unsigned char), rc, fp);
fwrite (data, sizeof (unsigned char), nbytes, fp);
fclose (fp);
}

View File

@ -22,8 +22,9 @@ int test_dump_memory (const char* name, const char* filename)
}
message ("device_dump\n");
rc = device_dump (device, data, sizeof (data));
if (rc < 0) {
unsigned int nbytes = 0;
rc = device_dump (device, data, sizeof (data), &nbytes);
if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read memory.");
device_close (device);
return rc;
@ -32,7 +33,7 @@ int test_dump_memory (const char* name, const char* filename)
message ("Dumping data\n");
FILE* fp = fopen (filename, "wb");
if (fp != NULL) {
fwrite (data, sizeof (unsigned char), rc, fp);
fwrite (data, sizeof (unsigned char), nbytes, fp);
fclose (fp);
}

View File

@ -22,8 +22,9 @@ int test_dump_memory (const char* name, const char* filename)
}
message ("device_dump\n");
rc = device_dump (device, data, sizeof (data));
if (rc < 0) {
unsigned int nbytes = 0;
rc = device_dump (device, data, sizeof (data), &nbytes);
if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read memory.");
device_close (device);
return rc;
@ -32,7 +33,7 @@ int test_dump_memory (const char* name, const char* filename)
message ("Dumping data\n");
FILE* fp = fopen (filename, "wb");
if (fp != NULL) {
fwrite (data, sizeof (unsigned char), rc, fp);
fwrite (data, sizeof (unsigned char), nbytes, fp);
fclose (fp);
}

View File

@ -49,8 +49,9 @@ int test_dump_memory (const char* filename)
}
message ("device_dump\n");
rc = device_dump (device, data, size);
if (rc < 0) {
unsigned int nbytes = 0;
rc = device_dump (device, data, size, &nbytes);
if (rc != DEVICE_STATUS_SUCCESS) {
WARNING ("Cannot read data.");
device_close (device);
free (data);
@ -60,7 +61,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), rc, fp);
fwrite (data, sizeof (unsigned char), nbytes, fp);
fclose (fp);
}

View File

@ -30,7 +30,7 @@ struct device_backend_t {
device_status_t (*write) (device_t *device, unsigned int address, const unsigned char data[], unsigned int size);
device_status_t (*dump) (device_t *device, unsigned char data[], unsigned int size);
device_status_t (*dump) (device_t *device, unsigned char data[], unsigned int size, unsigned int *result);
device_status_t (*foreach) (device_t *device, dive_callback_t callback, void *userdata);

View File

@ -90,7 +90,7 @@ device_write (device_t *device, unsigned int address, const unsigned char data[]
device_status_t
device_dump (device_t *device, unsigned char data[], unsigned int size)
device_dump (device_t *device, unsigned char data[], unsigned int size, unsigned int *result)
{
if (device == NULL)
return DEVICE_STATUS_UNSUPPORTED;
@ -98,7 +98,7 @@ device_dump (device_t *device, unsigned char data[], unsigned int size)
if (device->backend->dump == NULL)
return DEVICE_STATUS_UNSUPPORTED;
return device->backend->dump (device, data, size);
return device->backend->dump (device, data, size, result);
}

View File

@ -52,7 +52,7 @@ device_status_t device_read (device_t *device, unsigned int address, unsigned ch
device_status_t device_write (device_t *device, unsigned int address, const unsigned char data[], unsigned int size);
device_status_t device_dump (device_t *device, unsigned char data[], unsigned int size);
device_status_t device_dump (device_t *device, unsigned char data[], unsigned int size, unsigned int *result);
device_status_t device_foreach (device_t *device, dive_callback_t callback, void *userdata);

View File

@ -381,7 +381,7 @@ oceanic_atom2_read_ringbuffer (device_t *abstract, unsigned int address, unsigne
}
static device_status_t
oceanic_atom2_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
oceanic_atom2_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result)
{
if (! device_is_oceanic_atom2 (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
@ -393,7 +393,10 @@ oceanic_atom2_device_dump (device_t *abstract, unsigned char data[], unsigned in
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return OCEANIC_ATOM2_MEMORY_SIZE;
if (result)
*result = OCEANIC_ATOM2_MEMORY_SIZE;
return DEVICE_STATUS_SUCCESS;
}

View File

@ -187,7 +187,7 @@ reefnet_sensuspro_device_handshake (device_t *abstract, unsigned char *data, uns
static device_status_t
reefnet_sensuspro_device_dump (device_t *abstract, unsigned char *data, unsigned int size)
reefnet_sensuspro_device_dump (device_t *abstract, unsigned char *data, unsigned int size, unsigned int *result)
{
reefnet_sensuspro_device_t *device = (reefnet_sensuspro_device_t*) abstract;
@ -239,7 +239,10 @@ reefnet_sensuspro_device_dump (device_t *abstract, unsigned char *data, unsigned
return DEVICE_STATUS_MEMORY;
}
return REEFNET_SENSUSPRO_MEMORY_SIZE;
if (result)
*result = REEFNET_SENSUSPRO_MEMORY_SIZE;
return DEVICE_STATUS_SUCCESS;
}
@ -253,8 +256,8 @@ reefnet_sensuspro_device_foreach (device_t *abstract, dive_callback_t callback,
unsigned char data[REEFNET_SENSUSPRO_MEMORY_SIZE] = {0};
int rc = reefnet_sensuspro_device_dump (abstract, data, sizeof (data));
if (rc < 0)
int rc = reefnet_sensuspro_device_dump (abstract, data, sizeof (data), NULL);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return reefnet_sensuspro_extract_dives (data, sizeof (data), callback, userdata, device->timestamp);

View File

@ -342,7 +342,7 @@ reefnet_sensusultra_page (reefnet_sensusultra_device_t *device, unsigned char *d
static device_status_t
reefnet_sensusultra_device_dump (device_t *abstract, unsigned char *data, unsigned int size)
reefnet_sensusultra_device_dump (device_t *abstract, unsigned char *data, unsigned int size, unsigned int *result)
{
reefnet_sensusultra_device_t *device = (reefnet_sensusultra_device_t*) abstract;
@ -382,7 +382,10 @@ reefnet_sensusultra_device_dump (device_t *abstract, unsigned char *data, unsign
npages++;
}
return REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE;
if (result)
*result = REEFNET_SENSUSULTRA_MEMORY_DATA_SIZE;
return DEVICE_STATUS_SUCCESS;
}

View File

@ -365,7 +365,7 @@ suunto_d9_device_write (device_t *abstract, unsigned int address, const unsigned
static device_status_t
suunto_d9_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
suunto_d9_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result)
{
if (! device_is_suunto_d9 (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
@ -381,7 +381,10 @@ suunto_d9_device_dump (device_t *abstract, unsigned char data[], unsigned int si
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return SUUNTO_D9_MEMORY_SIZE;
if (result)
*result = SUUNTO_D9_MEMORY_SIZE;
return DEVICE_STATUS_SUCCESS;
}

View File

@ -118,7 +118,7 @@ suunto_eon_device_close (device_t *abstract)
static device_status_t
suunto_eon_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
suunto_eon_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result)
{
suunto_eon_device_t *device = (suunto_eon_device_t*) abstract;
@ -162,7 +162,10 @@ suunto_eon_device_dump (device_t *abstract, unsigned char data[], unsigned int s
return DEVICE_STATUS_MEMORY;
}
return SUUNTO_EON_MEMORY_SIZE;
if (result)
*result = SUUNTO_EON_MEMORY_SIZE;
return DEVICE_STATUS_SUCCESS;
}
@ -171,8 +174,8 @@ suunto_eon_device_foreach (device_t *abstract, dive_callback_t callback, void *u
{
unsigned char data[SUUNTO_EON_MEMORY_SIZE] = {0};
int rc = suunto_eon_device_dump (abstract, data, sizeof (data));
if (rc < 0)
int rc = suunto_eon_device_dump (abstract, data, sizeof (data), NULL);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return suunto_eon_extract_dives (data, sizeof (data), callback, userdata);

View File

@ -405,7 +405,7 @@ suunto_vyper_device_write (device_t *abstract, unsigned int address, const unsig
device_status_t
suunto_vyper_read_dive (device_t *abstract, unsigned char data[], unsigned int size, int init, device_progress_state_t *progress)
suunto_vyper_read_dive (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result, int init, device_progress_state_t *progress)
{
suunto_vyper_device_t *device = (suunto_vyper_device_t*) abstract;
@ -494,7 +494,9 @@ suunto_vyper_read_dive (device_t *abstract, unsigned char data[], unsigned int s
}
message("\"\n");
#endif
return 0;
if (result)
*result = 0;
return DEVICE_STATUS_SUCCESS;
}
progress_event (progress, DEVICE_EVENT_PROGRESS, len);
@ -521,19 +523,22 @@ suunto_vyper_read_dive (device_t *abstract, unsigned char data[], unsigned int s
message("\"\n");
#endif
return nbytes;
if (result)
*result = nbytes;
return DEVICE_STATUS_SUCCESS;
}
device_status_t
suunto_vyper_device_read_dive (device_t *abstract, unsigned char data[], unsigned int size, int init)
suunto_vyper_device_read_dive (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result, int init)
{
return suunto_vyper_read_dive (abstract, data, size, init, NULL);
return suunto_vyper_read_dive (abstract, data, size, result, init, NULL);
}
static device_status_t
suunto_vyper_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
suunto_vyper_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result)
{
if (! device_is_suunto_vyper (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
@ -549,7 +554,10 @@ suunto_vyper_device_dump (device_t *abstract, unsigned char data[], unsigned int
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return SUUNTO_VYPER_MEMORY_SIZE;
if (result)
*result = SUUNTO_VYPER_MEMORY_SIZE;
return DEVICE_STATUS_SUCCESS;
}
@ -573,18 +581,19 @@ suunto_vyper_device_foreach (device_t *abstract, dive_callback_t callback, void
int rc = 0;
unsigned int ndives = 0;
unsigned int offset = 0;
while ((rc = suunto_vyper_read_dive (abstract, data + offset, sizeof (data) - offset, (ndives == 0), &progress)) > 0) {
if (callback && !callback (data + offset, rc, userdata))
unsigned int nbytes = 0;
while ((rc = suunto_vyper_read_dive (abstract, data + offset, sizeof (data) - offset, &nbytes, (ndives == 0), &progress)) == DEVICE_STATUS_SUCCESS) {
if (nbytes == 0)
return DEVICE_STATUS_SUCCESS;
if (callback && !callback (data + offset, nbytes, userdata))
return DEVICE_STATUS_SUCCESS;
ndives++;
offset += rc;
offset += nbytes;
}
if (rc != 0)
return rc;
return DEVICE_STATUS_SUCCESS;
return rc;
}

View File

@ -20,7 +20,7 @@ device_status_t
suunto_vyper_device_detect_interface (device_t *device);
device_status_t
suunto_vyper_device_read_dive (device_t *device, unsigned char data[], unsigned int size, int init);
suunto_vyper_device_read_dive (device_t *device, unsigned char data[], unsigned int size, unsigned int *result, int init);
device_status_t
suunto_vyper_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);

View File

@ -352,7 +352,7 @@ suunto_vyper2_device_write (device_t *abstract, unsigned int address, const unsi
static device_status_t
suunto_vyper2_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
suunto_vyper2_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result)
{
if (! device_is_suunto_vyper2 (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
@ -368,7 +368,10 @@ suunto_vyper2_device_dump (device_t *abstract, unsigned char data[], unsigned in
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return SUUNTO_VYPER2_MEMORY_SIZE;
if (result)
*result = SUUNTO_VYPER2_MEMORY_SIZE;
return DEVICE_STATUS_SUCCESS;
}

View File

@ -138,7 +138,7 @@ uwatec_aladin_device_set_timestamp (device_t *abstract, unsigned int timestamp)
static device_status_t
uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result)
{
uwatec_aladin_device_t *device = (uwatec_aladin_device_t*) abstract;
@ -197,7 +197,10 @@ uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned in
return DEVICE_STATUS_MEMORY;
}
return UWATEC_ALADIN_MEMORY_SIZE;
if (result)
*result = UWATEC_ALADIN_MEMORY_SIZE;
return DEVICE_STATUS_SUCCESS;
}
@ -211,8 +214,8 @@ uwatec_aladin_device_foreach (device_t *abstract, dive_callback_t callback, void
unsigned char data[UWATEC_ALADIN_MEMORY_SIZE] = {0};
int rc = uwatec_aladin_device_dump (abstract, data, sizeof (data));
if (rc < 0)
int rc = uwatec_aladin_device_dump (abstract, data, sizeof (data), NULL);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return uwatec_aladin_extract_dives (data, sizeof (data), callback, userdata, device->timestamp);

View File

@ -158,7 +158,7 @@ uwatec_memomouse_confirm (uwatec_memomouse_device_t *device, unsigned char value
static device_status_t
uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char data[], unsigned int size)
uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char data[], unsigned int size, unsigned int *result)
{
assert (size >= 126 + 2);
@ -197,16 +197,20 @@ uwatec_memomouse_read_packet (uwatec_memomouse_device_t *device, unsigned char d
return DEVICE_STATUS_PROTOCOL;
}
return len;
if (result)
*result = len;
return DEVICE_STATUS_SUCCESS;
}
static device_status_t
uwatec_memomouse_read_packet_outer (uwatec_memomouse_device_t *device, unsigned char data[], unsigned int size)
uwatec_memomouse_read_packet_outer (uwatec_memomouse_device_t *device, unsigned char data[], unsigned int size, unsigned int *result)
{
int rc = 0;
unsigned int length = 0;
unsigned char package[126 + 2] = {0};
while ((rc = uwatec_memomouse_read_packet (device, package, sizeof (package))) < 0) {
while ((rc = uwatec_memomouse_read_packet (device, package, sizeof (package), &length)) != DEVICE_STATUS_SUCCESS) {
// Automatically discard a corrupted packet,
// and request a new one.
if (rc != DEVICE_STATUS_PROTOCOL)
@ -222,21 +226,24 @@ uwatec_memomouse_read_packet_outer (uwatec_memomouse_device_t *device, unsigned
}
#ifndef NDEBUG
message ("package(%i)=\"", rc);
for (unsigned int i = 0; i < rc; ++i) {
message ("package(%i)=\"", length);
for (unsigned int i = 0; i < length; ++i) {
message ("%02x", package[i + 1]);
}
message ("\"\n");
#endif
if (size >= rc) {
memcpy (data, package + 1, rc);
if (size >= length) {
memcpy (data, package + 1, length);
} else {
WARNING ("Insufficient buffer space available.");
return DEVICE_STATUS_MEMORY;
}
return rc;
if (result)
*result = length;
return DEVICE_STATUS_SUCCESS;
}
@ -244,19 +251,20 @@ static device_status_t
uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned char *data[], unsigned int *size, device_progress_state_t *progress)
{
// Read the first package.
unsigned int length = 0;
unsigned char package[126] = {0};
int rca = uwatec_memomouse_read_packet_outer (device, package, sizeof (package));
if (rca < 0)
return rca;
int rc = uwatec_memomouse_read_packet_outer (device, package, sizeof (package), &length);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
// Accept the package.
int rcb = uwatec_memomouse_confirm (device, ACK);
if (rcb != DEVICE_STATUS_SUCCESS)
return rcb;
rc = uwatec_memomouse_confirm (device, ACK);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
// Verify the first package contains at least
// the size of the inner package.
if (rca < 2) {
if (length < 2) {
WARNING ("First package is too small.");
return DEVICE_STATUS_PROTOCOL;
}
@ -265,7 +273,7 @@ uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned
unsigned int total = package[0] + (package[1] << 8) + 3;
progress_set_maximum (progress, total);
progress_event (progress, DEVICE_EVENT_PROGRESS, rca);
progress_event (progress, DEVICE_EVENT_PROGRESS, length);
// Allocate memory for the entire package.
unsigned char *buffer = malloc (total * sizeof (unsigned char));
@ -275,28 +283,28 @@ uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned
}
// Copy the first package to the new memory buffer.
memcpy (buffer, package, rca);
memcpy (buffer, package, length);
// Read the remaining packages.
unsigned int nbytes = rca;
unsigned int nbytes = length;
while (nbytes < total) {
// Read the package.
rca = uwatec_memomouse_read_packet_outer (device, buffer + nbytes, total - nbytes);
if (rca < 0) {
rc = uwatec_memomouse_read_packet_outer (device, buffer + nbytes, total - nbytes, &length);
if (rc != DEVICE_STATUS_SUCCESS) {
free (buffer);
return rca;
return rc;
}
// Accept the package.
rcb = uwatec_memomouse_confirm (device, ACK);
if (rcb != DEVICE_STATUS_SUCCESS) {
rc = uwatec_memomouse_confirm (device, ACK);
if (rc != DEVICE_STATUS_SUCCESS) {
free (buffer);
return rcb;
return rc;
}
progress_event (progress, DEVICE_EVENT_PROGRESS, rca);
progress_event (progress, DEVICE_EVENT_PROGRESS, length);
nbytes += rca;
nbytes += length;
}
// Verify the checksum.
@ -403,7 +411,7 @@ uwatec_memomouse_dump (uwatec_memomouse_device_t *device, unsigned char *data[],
static device_status_t
uwatec_memomouse_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
uwatec_memomouse_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result)
{
uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract;
@ -426,7 +434,10 @@ uwatec_memomouse_device_dump (device_t *abstract, unsigned char data[], unsigned
free (buffer);
return length - 3;
if (result)
*result = length - 3;
return DEVICE_STATUS_SUCCESS;
}

View File

@ -392,7 +392,7 @@ uwatec_smart_dump (uwatec_smart_device_t *device, unsigned char *data[], unsigne
static device_status_t
uwatec_smart_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
uwatec_smart_device_dump (device_t *abstract, unsigned char data[], unsigned int size, unsigned int *result)
{
uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract;
@ -415,7 +415,10 @@ uwatec_smart_device_dump (device_t *abstract, unsigned char data[], unsigned int
free (buffer);
return length;
if (result)
*result = length;
return DEVICE_STATUS_SUCCESS;
}