From 56b52f8fad0f172ccc4554ab92184bb7d25f7f6c Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 27 Dec 2008 16:17:35 +0000 Subject: [PATCH] Take care of the init and quit command internally. --- examples/oceanic_atom2_test.c | 16 ----- src/libdivecomputer.symbols | 2 - src/oceanic_atom2.c | 132 +++++++++++++++++----------------- src/oceanic_atom2.h | 6 -- 4 files changed, 64 insertions(+), 92 deletions(-) diff --git a/examples/oceanic_atom2_test.c b/examples/oceanic_atom2_test.c index 021b370..9b368b2 100644 --- a/examples/oceanic_atom2_test.c +++ b/examples/oceanic_atom2_test.c @@ -42,14 +42,6 @@ test_dump_memory (const char* name, const char* filename) return rc; } - message ("oceanic_atom2_device_handshake\n"); - rc = oceanic_atom2_device_handshake (device); - if (rc != DEVICE_STATUS_SUCCESS) { - WARNING ("Handshake failed."); - device_close (device); - return rc; - } - message ("device_version\n"); unsigned char version[OCEANIC_ATOM2_PACKET_SIZE] = {0}; rc = device_version (device, version, sizeof (version)); @@ -82,14 +74,6 @@ test_dump_memory (const char* name, const char* filename) return rc; } - message ("oceanic_atom2_device_quit\n"); - rc = oceanic_atom2_device_quit (device); - if (rc != DEVICE_STATUS_SUCCESS) { - WARNING ("Quit failed."); - device_close (device); - return rc; - } - message ("device_close\n"); rc = device_close (device); if (rc != DEVICE_STATUS_SUCCESS) { diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index 4f2fb5c..1304224 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -31,9 +31,7 @@ message_set_logfile #ifdef SERIAL mares_nemo_device_open mares_nemo_extract_dives -oceanic_atom2_device_handshake oceanic_atom2_device_open -oceanic_atom2_device_quit oceanic_veo250_device_open oceanic_veo250_device_keepalive oceanic_vtpro_device_open diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index bd5de66..fb3f617 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -173,6 +173,64 @@ oceanic_atom2_transfer (oceanic_atom2_device_t *device, const unsigned char comm } +static device_status_t +oceanic_atom2_init (oceanic_atom2_device_t *device) +{ + // Send the command to the dive computer. + unsigned char command[3] = {0xA8, 0x99, 0x00}; + device_status_t rc = oceanic_atom2_send (device, command, sizeof (command)); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Failed to send the command."); + return rc; + } + + // Receive the answer of the dive computer. + unsigned char answer[3] = {0}; + int n = serial_read (device->port, answer, sizeof (answer)); + if (n != sizeof (answer)) { + WARNING ("Failed to receive the answer."); + return EXITCODE (n); + } + + // Verify the answer. + if (answer[0] != NAK || answer[1] != NAK || answer[2] != NAK) { + WARNING ("Unexpected answer byte(s)."); + return DEVICE_STATUS_PROTOCOL; + } + + return DEVICE_STATUS_SUCCESS; +} + + +static device_status_t +oceanic_atom2_quit (oceanic_atom2_device_t *device) +{ + // Send the command to the dive computer. + unsigned char command[4] = {0x6A, 0x05, 0xA5, 0x00}; + device_status_t rc = oceanic_atom2_send (device, command, sizeof (command)); + if (rc != DEVICE_STATUS_SUCCESS) { + WARNING ("Failed to send the command."); + return rc; + } + + // Receive the answer of the dive computer. + unsigned char answer[1] = {0}; + int n = serial_read (device->port, answer, sizeof (answer)); + if (n != sizeof (answer)) { + WARNING ("Failed to receive the answer."); + return EXITCODE (n); + } + + // Verify the answer. + if (answer[0] != 0xA5) { + WARNING ("Unexpected answer byte(s)."); + return DEVICE_STATUS_PROTOCOL; + } + + return DEVICE_STATUS_SUCCESS; +} + + device_status_t oceanic_atom2_device_open (device_t **out, const char* name) { @@ -223,6 +281,9 @@ oceanic_atom2_device_open (device_t **out, const char* name) // Make sure everything is in a sane state. serial_flush (device->port, SERIAL_QUEUE_BOTH); + // Send the init command. + oceanic_atom2_init (device); + *out = (device_t*) device; return DEVICE_STATUS_SUCCESS; @@ -237,6 +298,9 @@ oceanic_atom2_device_close (device_t *abstract) if (! device_is_oceanic_atom2 (abstract)) return DEVICE_STATUS_TYPE_MISMATCH; + // Send the quit command. + oceanic_atom2_quit (device); + // Close the device. if (serial_close (device->port) == -1) { free (device); @@ -250,74 +314,6 @@ oceanic_atom2_device_close (device_t *abstract) } -device_status_t -oceanic_atom2_device_handshake (device_t *abstract) -{ - oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract; - - if (! device_is_oceanic_atom2 (abstract)) - return DEVICE_STATUS_TYPE_MISMATCH; - - // Send the command to the dive computer. - unsigned char command[3] = {0xA8, 0x99, 0x00}; - device_status_t rc = oceanic_atom2_send (device, command, sizeof (command)); - if (rc != DEVICE_STATUS_SUCCESS) { - WARNING ("Failed to send the command."); - return rc; - } - - // Receive the answer of the dive computer. - unsigned char answer[3] = {0}; - int n = serial_read (device->port, answer, sizeof (answer)); - if (n != sizeof (answer)) { - WARNING ("Failed to receive the answer."); - return EXITCODE (n); - } - - // Verify the answer. - if (answer[0] != NAK || answer[1] != NAK || answer[2] != NAK) { - WARNING ("Unexpected answer byte(s)."); - return DEVICE_STATUS_PROTOCOL; - } - - return DEVICE_STATUS_SUCCESS; -} - - -device_status_t -oceanic_atom2_device_quit (device_t *abstract) -{ - oceanic_atom2_device_t *device = (oceanic_atom2_device_t*) abstract; - - if (! device_is_oceanic_atom2 (abstract)) - return DEVICE_STATUS_TYPE_MISMATCH; - - // Send the command to the dive computer. - unsigned char command[4] = {0x6A, 0x05, 0xA5, 0x00}; - device_status_t rc = oceanic_atom2_send (device, command, sizeof (command)); - if (rc != DEVICE_STATUS_SUCCESS) { - WARNING ("Failed to send the command."); - return rc; - } - - // Receive the answer of the dive computer. - unsigned char answer[1] = {0}; - int n = serial_read (device->port, answer, sizeof (answer)); - if (n != sizeof (answer)) { - WARNING ("Failed to receive the answer."); - return EXITCODE (n); - } - - // Verify the answer. - if (answer[0] != 0xA5) { - WARNING ("Unexpected answer byte(s)."); - return DEVICE_STATUS_PROTOCOL; - } - - return DEVICE_STATUS_SUCCESS; -} - - static device_status_t oceanic_atom2_device_version (device_t *abstract, unsigned char data[], unsigned int size) { diff --git a/src/oceanic_atom2.h b/src/oceanic_atom2.h index 8e12c08..8226bba 100644 --- a/src/oceanic_atom2.h +++ b/src/oceanic_atom2.h @@ -34,12 +34,6 @@ extern "C" { device_status_t oceanic_atom2_device_open (device_t **device, const char* name); -device_status_t -oceanic_atom2_device_handshake (device_t *device); - -device_status_t -oceanic_atom2_device_quit (device_t *device); - #ifdef __cplusplus } #endif /* __cplusplus */