From 2a1cd50910de705aa5ca7d3b8f2ba10f296a8c2b Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Wed, 26 Mar 2008 09:53:42 +0000 Subject: [PATCH] Implemented the callback function to read individual dives. --- suunto_vyper.c | 31 ++++++++++++++++++++++++++ suunto_vyper.h | 2 ++ suunto_vyper_test.c | 54 +++++---------------------------------------- 3 files changed, 39 insertions(+), 48 deletions(-) diff --git a/suunto_vyper.c b/suunto_vyper.c index f320ae5..9273ea7 100644 --- a/suunto_vyper.c +++ b/suunto_vyper.c @@ -502,3 +502,34 @@ suunto_vyper_read_dive (vyper *device, unsigned char data[], unsigned int size, return nbytes; } + + +int +suunto_vyper_read_dives (vyper *device, dive_callback_t callback, void *userdata) +{ + if (device == NULL) + return SUUNTO_ERROR; + + // The memory layout of the Spyder is different from the Vyper + // (and all other compatible dive computers). The Spyder has + // the largest ring buffer for the profile memory, so we use + // that value as the maximum size of the memory buffer. + + unsigned char data[SUUNTO_VYPER_MEMORY_SIZE - 0x4C] = {0}; + + int rc = 0; + unsigned int ndives = 0; + unsigned int offset = 0; + while ((rc = suunto_vyper_read_dive (device, data + offset, sizeof (data) - offset, (ndives == 0))) > 0) { + if (callback) + callback (data + offset, rc, userdata); + + ndives++; + offset += rc; + } + + if (rc != 0) + return rc; + + return SUUNTO_SUCCESS; +} diff --git a/suunto_vyper.h b/suunto_vyper.h index 744539a..659baf1 100644 --- a/suunto_vyper.h +++ b/suunto_vyper.h @@ -24,6 +24,8 @@ int suunto_vyper_read_memory (vyper *device, unsigned int address, unsigned char int suunto_vyper_write_memory (vyper *device, unsigned int address, const unsigned char data[], unsigned int size); +int suunto_vyper_read_dives (vyper *device, dive_callback_t callback, void *userdata); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/suunto_vyper_test.c b/suunto_vyper_test.c index db40396..25dea52 100644 --- a/suunto_vyper_test.c +++ b/suunto_vyper_test.c @@ -10,9 +10,8 @@ message ("%s:%d: %s\n", __FILE__, __LINE__, expr); \ } -int test_dump_sdm16 (const char* name, unsigned int delay, const char* filename) +int test_dump_sdm (const char* name, unsigned int delay) { - unsigned char data[SUUNTO_VYPER_MEMORY_SIZE] = {0}; vyper *device = NULL; message ("suunto_vyper_open\n"); @@ -32,54 +31,13 @@ int test_dump_sdm16 (const char* name, unsigned int delay, const char* filename) return rc; } - message ("suunto_vyper_read_memory\n"); - rc = suunto_vyper_read_memory (device, 0x24, data + 0x24, 1); + message ("suunto_vyper_read_dives\n"); + rc = suunto_vyper_read_dives (device, NULL, NULL); if (rc != SUUNTO_SUCCESS) { - WARNING ("Cannot identify computer."); + WARNING ("Cannot read dives."); suunto_vyper_close (device); return rc; } - rc = suunto_vyper_read_memory (device, 0x1E, data + 0x1E, 14); - if (rc != SUUNTO_SUCCESS) { - WARNING ("Cannot read memory."); - suunto_vyper_close (device); - return rc; - } - rc = suunto_vyper_read_memory (device, 0x2C, data + 0x2C, 32); - if (rc != SUUNTO_SUCCESS) { - WARNING ("Cannot read memory."); - suunto_vyper_close (device); - return rc; - } - rc = suunto_vyper_read_memory (device, 0x53, data + 0x53, 30); - if (rc != SUUNTO_SUCCESS) { - WARNING ("Cannot read memory."); - suunto_vyper_close (device); - return rc; - } - - message ("suunto_vyper_read_dive\n"); - int ndives = 0; - int offset = 0x71; - do { - message ("Reading dive #%d.\n", ndives + 1); - rc = suunto_vyper_read_dive (device, data + offset, sizeof (data) - offset, (ndives == 0)); - if (rc < 0) { - WARNING ("Cannot read dive."); - suunto_vyper_close (device); - return rc; - } - message ("Returned %i bytes at offset 0x%04x.\n", rc, offset); - ndives++; - offset += rc; - } while (rc > 0); - - message ("Dumping data\n"); - FILE *fp = fopen (filename, "wb"); - if (fp != NULL) { - fwrite (data, sizeof (unsigned char), sizeof (data), fp); - fclose (fp); - } message ("suunto_vyper_close\n"); rc = suunto_vyper_close (device); @@ -179,12 +137,12 @@ int main(int argc, char *argv[]) message ("DEVICE=%s, DELAY=%i\n", name, delay); - int a = test_dump_sdm16 (name, delay, "VYPER.SDM"); + int a = test_dump_sdm (name, delay); int b = test_dump_memory (name, delay, "VYPER.DMP"); message ("\nSUMMARY\n"); message ("-------\n"); - message ("test_dump_sdm16: %s\n", errmsg (a)); + message ("test_dump_sdm: %s\n", errmsg (a)); message ("test_dump_memory: %s\n", errmsg (b)); message_set_logfile (NULL);