Implement the device_foreach() function on top of device_dump().

This commit is contained in:
Jef Driesen 2008-07-22 07:57:00 +00:00
parent 5ae3b98d5d
commit 44e76cfa8e
5 changed files with 161 additions and 34 deletions

View File

@ -266,6 +266,19 @@ reefnet_sensuspro_device_dump (device_t *abstract, unsigned char *data, unsigned
} }
static device_status_t
reefnet_sensuspro_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
{
unsigned char data[REEFNET_SENSUSPRO_MEMORY_SIZE] = {0};
int rc = reefnet_sensuspro_device_dump (abstract, data, sizeof (data));
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return reefnet_sensuspro_extract_dives (data, sizeof (data), callback, userdata);
}
device_status_t device_status_t
reefnet_sensuspro_device_write_interval (device_t *abstract, unsigned char interval) reefnet_sensuspro_device_write_interval (device_t *abstract, unsigned char interval)
{ {
@ -346,6 +359,6 @@ static const device_backend_t reefnet_sensuspro_device_backend = {
NULL, /* read */ NULL, /* read */
NULL, /* write */ NULL, /* write */
reefnet_sensuspro_device_dump, /* dump */ reefnet_sensuspro_device_dump, /* dump */
NULL, /* foreach */ reefnet_sensuspro_device_foreach, /* foreach */
reefnet_sensuspro_device_close /* close */ reefnet_sensuspro_device_close /* close */
}; };

View File

@ -170,6 +170,19 @@ suunto_eon_device_dump (device_t *abstract, unsigned char data[], unsigned int s
} }
static device_status_t
suunto_eon_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
{
unsigned char data[SUUNTO_EON_MEMORY_SIZE] = {0};
int rc = suunto_eon_device_dump (abstract, data, sizeof (data));
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return suunto_eon_extract_dives (data, sizeof (data), callback, userdata);
}
device_status_t device_status_t
suunto_eon_device_write_name (device_t *abstract, unsigned char data[], unsigned int size) suunto_eon_device_write_name (device_t *abstract, unsigned char data[], unsigned int size)
{ {
@ -239,6 +252,6 @@ static const device_backend_t suunto_eon_device_backend = {
NULL, /* read */ NULL, /* read */
NULL, /* write */ NULL, /* write */
suunto_eon_device_dump, /* dump */ suunto_eon_device_dump, /* dump */
NULL, /* foreach */ suunto_eon_device_foreach, /* foreach */
suunto_eon_device_close /* close */ suunto_eon_device_close /* close */
}; };

View File

@ -214,6 +214,19 @@ uwatec_aladin_device_dump (device_t *abstract, unsigned char data[], unsigned in
} }
static device_status_t
uwatec_aladin_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
{
unsigned char data[UWATEC_ALADIN_MEMORY_SIZE] = {0};
int rc = uwatec_aladin_device_dump (abstract, data, sizeof (data));
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
return uwatec_aladin_extract_dives (data, sizeof (data), callback, userdata);
}
#define HEADER 4 #define HEADER 4
device_status_t device_status_t
@ -325,6 +338,6 @@ static const device_backend_t uwatec_aladin_device_backend = {
NULL, /* read */ NULL, /* read */
NULL, /* write */ NULL, /* write */
uwatec_aladin_device_dump, /* dump */ uwatec_aladin_device_dump, /* dump */
NULL, /* foreach */ uwatec_aladin_device_foreach, /* foreach */
uwatec_aladin_device_close /* close */ uwatec_aladin_device_close /* close */
}; };

View File

@ -268,7 +268,7 @@ uwatec_memomouse_read_packet_outer (uwatec_memomouse_device_t *device, unsigned
static device_status_t static device_status_t
uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned char data[], unsigned int size) uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned char *data[], unsigned int *size)
{ {
// Read the first package. // Read the first package.
unsigned char package[126] = {0}; unsigned char package[126] = {0};
@ -329,28 +329,16 @@ uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned
return DEVICE_STATUS_PROTOCOL; return DEVICE_STATUS_PROTOCOL;
} }
// Copy the package to the output buffer. *data = buffer;
if (total - 3 <= size) { *size = total;
memcpy (data, buffer + 2, total - 3);
} else {
WARNING ("Insufficient buffer space available.");
return DEVICE_STATUS_MEMORY;
}
free (buffer); return DEVICE_STATUS_SUCCESS;
return total - 3;
} }
static device_status_t static device_status_t
uwatec_memomouse_device_dump (device_t *abstract, unsigned char data[], unsigned int size) uwatec_memomouse_dump (uwatec_memomouse_device_t *device, unsigned char *data[], unsigned int *size)
{ {
uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract;
if (! device_is_uwatec_memomouse (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
// Waiting for greeting message. // Waiting for greeting message.
while (serial_get_received (device->port) == 0) { while (serial_get_received (device->port) == 0) {
// Flush the input buffer. // Flush the input buffer.
@ -365,11 +353,14 @@ uwatec_memomouse_device_dump (device_t *abstract, unsigned char data[], unsigned
} }
// Read the ID string. // Read the ID string.
unsigned char id[7] = {0}; unsigned int id_length = 0;
int rc = uwatec_memomouse_read_packet_inner (device, id, sizeof (id)); unsigned char *id_buffer = NULL;
if (rc < 0) int rc = uwatec_memomouse_read_packet_inner (device, &id_buffer, &id_length);
if (rc != DEVICE_STATUS_SUCCESS)
return rc; return rc;
free (id_buffer);
// Prepare the command. // Prepare the command.
unsigned char command [9] = { unsigned char command [9] = {
0x07, // Outer packet size. 0x07, // Outer packet size.
@ -427,6 +418,60 @@ uwatec_memomouse_device_dump (device_t *abstract, unsigned char data[], unsigned
} }
static device_status_t
uwatec_memomouse_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
{
uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract;
if (! device_is_uwatec_memomouse (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
unsigned int length = 0;
unsigned char *buffer = NULL;
int rc = uwatec_memomouse_dump (device, &buffer, &length);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
if (length - 3 <= size) {
memcpy (data, buffer + 2, length - 3);
} else {
WARNING ("Insufficient buffer space available.");
free (buffer);
return DEVICE_STATUS_MEMORY;
}
free (buffer);
return length - 3;
}
static device_status_t
uwatec_memomouse_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
{
uwatec_memomouse_device_t *device = (uwatec_memomouse_device_t*) abstract;
if (! device_is_uwatec_memomouse (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
unsigned int length = 0;
unsigned char *buffer = NULL;
int rc = uwatec_memomouse_dump (device, &buffer, &length);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
rc = uwatec_memomouse_extract_dives (buffer + 2, length - 3, callback, userdata);
if (rc != DEVICE_STATUS_SUCCESS) {
free (buffer);
return rc;
}
free (buffer);
return DEVICE_STATUS_SUCCESS;
}
device_status_t device_status_t
uwatec_memomouse_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata) uwatec_memomouse_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata)
{ {
@ -492,6 +537,6 @@ static const device_backend_t uwatec_memomouse_device_backend = {
NULL, /* read */ NULL, /* read */
NULL, /* write */ NULL, /* write */
uwatec_memomouse_device_dump, /* dump */ uwatec_memomouse_device_dump, /* dump */
NULL, /* foreach */ uwatec_memomouse_device_foreach, /* foreach */
uwatec_memomouse_device_close /* close */ uwatec_memomouse_device_close /* close */
}; };

View File

@ -300,13 +300,8 @@ uwatec_smart_device_version (device_t *abstract, unsigned char data[], unsigned
static device_status_t static device_status_t
uwatec_smart_device_dump (device_t *abstract, unsigned char data[], unsigned int size) uwatec_smart_dump (uwatec_smart_device_t *device, unsigned char *data[], unsigned int *size)
{ {
uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract;
if (! device_is_uwatec_smart (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
unsigned char command[9] = {0}; unsigned char command[9] = {0};
unsigned char answer[4] = {0}; unsigned char answer[4] = {0};
@ -331,7 +326,7 @@ uwatec_smart_device_dump (device_t *abstract, unsigned char data[], unsigned int
message ("handshake: length=%u\n", length); message ("handshake: length=%u\n", length);
if (length == 0) if (length == 0)
return 0; return DEVICE_STATUS_SUCCESS;
unsigned char *package = malloc (length * sizeof (unsigned char)); unsigned char *package = malloc (length * sizeof (unsigned char));
if (package == NULL) { if (package == NULL) {
@ -378,19 +373,67 @@ uwatec_smart_device_dump (device_t *abstract, unsigned char data[], unsigned int
message ("len=%u, rc=%i, nbytes=%u\n", len, rc, nbytes); message ("len=%u, rc=%i, nbytes=%u\n", len, rc, nbytes);
} }
*data = package;
*size = length;
return DEVICE_STATUS_SUCCESS;
}
static device_status_t
uwatec_smart_device_dump (device_t *abstract, unsigned char data[], unsigned int size)
{
uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract;
if (! device_is_uwatec_smart (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
unsigned int length = 0;
unsigned char *buffer = NULL;
int rc = uwatec_smart_dump (device, &buffer, &length);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
if (length <= size) { if (length <= size) {
memcpy (data, package, length); memcpy (data, buffer, length);
} else { } else {
WARNING ("Insufficient buffer space available."); WARNING ("Insufficient buffer space available.");
free (buffer);
return DEVICE_STATUS_MEMORY; return DEVICE_STATUS_MEMORY;
} }
free (package); free (buffer);
return length; return length;
} }
static device_status_t
uwatec_smart_device_foreach (device_t *abstract, dive_callback_t callback, void *userdata)
{
uwatec_smart_device_t *device = (uwatec_smart_device_t*) abstract;
if (! device_is_uwatec_smart (abstract))
return DEVICE_STATUS_TYPE_MISMATCH;
unsigned int length = 0;
unsigned char *buffer = NULL;
int rc = uwatec_smart_dump (device, &buffer, &length);
if (rc != DEVICE_STATUS_SUCCESS)
return rc;
rc = uwatec_smart_extract_dives (buffer, length, callback, userdata);
if (rc != DEVICE_STATUS_SUCCESS) {
free (buffer);
return rc;
}
free (buffer);
return DEVICE_STATUS_SUCCESS;
}
device_status_t device_status_t
uwatec_smart_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata) uwatec_smart_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata)
{ {
@ -430,6 +473,6 @@ static const device_backend_t uwatec_smart_device_backend = {
NULL, /* read */ NULL, /* read */
NULL, /* write */ NULL, /* write */
uwatec_smart_device_dump, /* dump */ uwatec_smart_device_dump, /* dump */
NULL, /* foreach */ uwatec_smart_device_foreach, /* foreach */
uwatec_smart_device_close /* close */ uwatec_smart_device_close /* close */
}; };