From 44e76cfa8ea635ee984e22df1b64dd49fc1a1a4a Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 22 Jul 2008 07:57:00 +0000 Subject: [PATCH] Implement the device_foreach() function on top of device_dump(). --- src/reefnet_sensuspro.c | 15 ++++++- src/suunto_eon.c | 15 ++++++- src/uwatec_aladin.c | 15 ++++++- src/uwatec_memomouse.c | 87 +++++++++++++++++++++++++++++++---------- src/uwatec_smart.c | 63 ++++++++++++++++++++++++----- 5 files changed, 161 insertions(+), 34 deletions(-) diff --git a/src/reefnet_sensuspro.c b/src/reefnet_sensuspro.c index 5f1140c..2e15897 100644 --- a/src/reefnet_sensuspro.c +++ b/src/reefnet_sensuspro.c @@ -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 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, /* write */ reefnet_sensuspro_device_dump, /* dump */ - NULL, /* foreach */ + reefnet_sensuspro_device_foreach, /* foreach */ reefnet_sensuspro_device_close /* close */ }; diff --git a/src/suunto_eon.c b/src/suunto_eon.c index d93a49a..da5de2d 100644 --- a/src/suunto_eon.c +++ b/src/suunto_eon.c @@ -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 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, /* write */ suunto_eon_device_dump, /* dump */ - NULL, /* foreach */ + suunto_eon_device_foreach, /* foreach */ suunto_eon_device_close /* close */ }; diff --git a/src/uwatec_aladin.c b/src/uwatec_aladin.c index 46727bb..4187b42 100644 --- a/src/uwatec_aladin.c +++ b/src/uwatec_aladin.c @@ -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 device_status_t @@ -325,6 +338,6 @@ static const device_backend_t uwatec_aladin_device_backend = { NULL, /* read */ NULL, /* write */ uwatec_aladin_device_dump, /* dump */ - NULL, /* foreach */ + uwatec_aladin_device_foreach, /* foreach */ uwatec_aladin_device_close /* close */ }; diff --git a/src/uwatec_memomouse.c b/src/uwatec_memomouse.c index 32a790f..67d1bcc 100644 --- a/src/uwatec_memomouse.c +++ b/src/uwatec_memomouse.c @@ -268,7 +268,7 @@ uwatec_memomouse_read_packet_outer (uwatec_memomouse_device_t *device, unsigned 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. unsigned char package[126] = {0}; @@ -329,28 +329,16 @@ uwatec_memomouse_read_packet_inner (uwatec_memomouse_device_t *device, unsigned return DEVICE_STATUS_PROTOCOL; } - // Copy the package to the output buffer. - if (total - 3 <= size) { - memcpy (data, buffer + 2, total - 3); - } else { - WARNING ("Insufficient buffer space available."); - return DEVICE_STATUS_MEMORY; - } + *data = buffer; + *size = total; - free (buffer); - - return total - 3; + return DEVICE_STATUS_SUCCESS; } 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. while (serial_get_received (device->port) == 0) { // Flush the input buffer. @@ -365,11 +353,14 @@ uwatec_memomouse_device_dump (device_t *abstract, unsigned char data[], unsigned } // Read the ID string. - unsigned char id[7] = {0}; - int rc = uwatec_memomouse_read_packet_inner (device, id, sizeof (id)); - if (rc < 0) + unsigned int id_length = 0; + unsigned char *id_buffer = NULL; + int rc = uwatec_memomouse_read_packet_inner (device, &id_buffer, &id_length); + if (rc != DEVICE_STATUS_SUCCESS) return rc; + free (id_buffer); + // Prepare the command. unsigned char command [9] = { 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 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, /* write */ uwatec_memomouse_device_dump, /* dump */ - NULL, /* foreach */ + uwatec_memomouse_device_foreach, /* foreach */ uwatec_memomouse_device_close /* close */ }; diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index 71c620b..a31c9ac 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -300,13 +300,8 @@ uwatec_smart_device_version (device_t *abstract, unsigned char data[], unsigned 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 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); if (length == 0) - return 0; + return DEVICE_STATUS_SUCCESS; unsigned char *package = malloc (length * sizeof (unsigned char)); 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); } + *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) { - memcpy (data, package, length); + memcpy (data, buffer, length); } else { WARNING ("Insufficient buffer space available."); + free (buffer); return DEVICE_STATUS_MEMORY; } - free (package); + free (buffer); 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 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, /* write */ uwatec_smart_device_dump, /* dump */ - NULL, /* foreach */ + uwatec_smart_device_foreach, /* foreach */ uwatec_smart_device_close /* close */ };