diff --git a/examples/uwatec_smart_test.c b/examples/uwatec_smart_test.c index 3af3b99..cd939e8 100644 --- a/examples/uwatec_smart_test.c +++ b/examples/uwatec_smart_test.c @@ -29,6 +29,25 @@ int test_dump_memory (const char* filename) return rc; } + message ("uwatec_smart_handshake\n"); + rc = uwatec_smart_handshake (device); + if (rc != UWATEC_SUCCESS) { + WARNING ("Handshake failed."); + uwatec_smart_close (device); + free (data); + return rc; + } + + message ("uwatec_smart_version\n"); + unsigned char version[UWATEC_SMART_VERSION_SIZE] = {0}; + rc = uwatec_smart_version (device, version, sizeof (version)); + if (rc != UWATEC_SUCCESS) { + WARNING ("Cannot identify computer."); + uwatec_smart_close (device); + free (data); + return rc; + } + message ("uwatec_smart_read\n"); rc = uwatec_smart_read (device, data, size); if (rc < 0) { diff --git a/src/uwatec_smart.c b/src/uwatec_smart.c index f8b2b7e..3c1f0dc 100644 --- a/src/uwatec_smart.c +++ b/src/uwatec_smart.c @@ -166,13 +166,13 @@ uwatec_smart_transfer (smart *device, const unsigned char command[], unsigned in int -uwatec_smart_read (smart *device, unsigned char data[], unsigned int size) +uwatec_smart_handshake (smart *device) { if (device == NULL) return UWATEC_ERROR; - unsigned char command[9] = {0}; - unsigned char answer[4] = {0}; + unsigned char command[5] = {0}; + unsigned char answer[1] = {0}; // Handshake (stage 1). @@ -207,12 +207,25 @@ uwatec_smart_read (smart *device, unsigned char data[], unsigned int size) WARNING ("Unexpected answer byte(s)."); return UWATEC_ERROR_PROTOCOL; } + + return UWATEC_SUCCESS; +} + + +int +uwatec_smart_version (smart *device, unsigned char data[], unsigned int size) +{ + if (device == NULL) + return UWATEC_ERROR; + + unsigned char command[1] = {0}; + unsigned char answer[UWATEC_SMART_VERSION_SIZE] = {0}; // Dive Computer Time. command[0] = 0x1A; - rc = uwatec_smart_transfer (device, command, 1, answer, 4); + int rc = uwatec_smart_transfer (device, command, 1, answer + 0, 4); if (rc != UWATEC_SUCCESS) return rc; @@ -231,23 +244,43 @@ uwatec_smart_read (smart *device, unsigned char data[], unsigned int size) command[0] = 0x14; - rc = uwatec_smart_transfer (device, command, 1, answer, 4); + rc = uwatec_smart_transfer (device, command, 1, answer + 4, 4); if (rc != UWATEC_SUCCESS) return rc; - unsigned int serial = answer[0] + (answer[1] << 8) + - (answer[2] << 16) + (answer[3] << 24); + unsigned int serial = answer[4] + (answer[5] << 8) + + (answer[6] << 16) + (answer[7] << 24); message ("handshake: serial=0x%08x\n", serial); // Dive Computer Model. command[0] = 0x10; - rc = uwatec_smart_transfer (device, command, 1, answer, 1); + rc = uwatec_smart_transfer (device, command, 1, answer + 8, 1); if (rc != UWATEC_SUCCESS) return rc; - message ("handshake: model=0x%02x\n", answer[0]); + message ("handshake: model=0x%02x\n", answer[8]); + + if (size >= UWATEC_SMART_VERSION_SIZE) { + memcpy (data, answer, UWATEC_SMART_VERSION_SIZE); + } else { + WARNING ("Insufficient buffer space available."); + return UWATEC_ERROR_MEMORY; + } + + return UWATEC_SUCCESS; +} + + +int +uwatec_smart_read (smart *device, unsigned char data[], unsigned int size) +{ + if (device == NULL) + return UWATEC_ERROR; + + unsigned char command[9] = {0}; + unsigned char answer[4] = {0}; // Data Length. @@ -261,7 +294,7 @@ uwatec_smart_read (smart *device, unsigned char data[], unsigned int size) command[7] = 0; command[8] = 0; - rc = uwatec_smart_transfer (device, command, 9, answer, 4); + int rc = uwatec_smart_transfer (device, command, 9, answer, 4); if (rc != UWATEC_SUCCESS) return rc; diff --git a/src/uwatec_smart.h b/src/uwatec_smart.h index 0724766..a18ac54 100644 --- a/src/uwatec_smart.h +++ b/src/uwatec_smart.h @@ -7,12 +7,18 @@ extern "C" { typedef struct smart smart; +#define UWATEC_SMART_VERSION_SIZE 9 + int uwatec_smart_open (smart **device); int uwatec_smart_close (smart *device); int uwatec_smart_set_timestamp (smart *device, unsigned int timestamp); +int uwatec_smart_handshake (smart *device); + +int uwatec_smart_version (smart *device, unsigned char data[], unsigned int size); + int uwatec_smart_read (smart *device, unsigned char data[], unsigned int size); int uwatec_smart_extract_dives (const unsigned char data[], unsigned int size, dive_callback_t callback, void *userdata);