diff --git a/examples/oceanic_atom2_test.c b/examples/oceanic_atom2_test.c index 4903ce1..b602e3a 100644 --- a/examples/oceanic_atom2_test.c +++ b/examples/oceanic_atom2_test.c @@ -20,6 +20,14 @@ int test_dump_memory (const char* name, const char* filename) return rc; } + message ("oceanic_atom2_handshake\n"); + rc = oceanic_atom2_handshake (device); + if (rc != OCEANIC_SUCCESS) { + WARNING ("Handshake failed."); + oceanic_atom2_close (device); + return rc; + } + message ("oceanic_atom2_read_version\n"); unsigned char version[OCEANIC_ATOM2_PACKET_SIZE] = {0}; rc = oceanic_atom2_read_version (device, version, sizeof (version)); @@ -52,6 +60,14 @@ int test_dump_memory (const char* name, const char* filename) return rc; } + message ("oceanic_atom2_quit\n"); + rc = oceanic_atom2_quit (device); + if (rc != OCEANIC_SUCCESS) { + WARNING ("Quit failed."); + oceanic_atom2_close (device); + return rc; + } + message ("oceanic_atom2_close\n"); rc = oceanic_atom2_close (device); if (rc != OCEANIC_SUCCESS) { diff --git a/src/oceanic_atom2.c b/src/oceanic_atom2.c index c396756..9bab504 100644 --- a/src/oceanic_atom2.c +++ b/src/oceanic_atom2.c @@ -155,24 +155,6 @@ oceanic_atom2_open (atom2 **out, const char* name) // Make sure everything is in a sane state. serial_flush (device->port, SERIAL_QUEUE_BOTH); - // Send the handshake to connect to the device. - unsigned char answer[3] = {0}; - unsigned char command[3] = {0xA8, 0x99, 0x00}; - rc = oceanic_atom2_transfer (device, command, sizeof (command), answer, sizeof (answer), 1); - if (rc != OCEANIC_SUCCESS) { - serial_close (device->port); - free (device); - return rc; - } - - // Verify the handshake. - if (answer[1] != 0xA5) { - WARNING ("Unexpected handshake byte(s)."); - serial_close (device->port); - free (device); - return OCEANIC_ERROR_PROTOCOL; - } - *out = device; return OCEANIC_SUCCESS; @@ -198,6 +180,63 @@ oceanic_atom2_close (atom2 *device) } +int +oceanic_atom2_handshake (atom2 *device) +{ + if (device == NULL) + return OCEANIC_ERROR; + + // Send the handshake to connect to the device. + unsigned char answer[3] = {0}; + unsigned char command[3] = {0xA8, 0x99, 0x00}; + int rc = oceanic_atom2_transfer (device, command, sizeof (command), answer, sizeof (answer), 1); + if (rc != OCEANIC_SUCCESS) + return rc; + + // Verify the handshake. + if (answer[1] != 0xA5) { + WARNING ("Unexpected handshake byte(s)."); + return OCEANIC_ERROR_PROTOCOL; + } + + return OCEANIC_SUCCESS; +} + + +int +oceanic_atom2_quit (atom2 *device) +{ + if (device == NULL) + return OCEANIC_ERROR; + + // Send the command to the dive computer. + unsigned char command[4] = {0x6A, 0x05, 0xA5, 0x00}; + int rc = oceanic_atom2_send (device, command, sizeof (command)); + if (rc != OCEANIC_SUCCESS) { + WARNING ("Failed to send the command."); + return rc; + } + + // Receive the answer of the dive computer. + unsigned char answer[1] = {0}; + rc = serial_read (device->port, answer, sizeof (answer)); + if (rc != sizeof (answer)) { + WARNING ("Failed to receive the answer."); + if (rc == -1) + return OCEANIC_ERROR_IO; + return OCEANIC_ERROR_TIMEOUT; + } + + // Verify the answer. + if (answer[0] != 0xA5) { + WARNING ("Unexpected answer byte(s)."); + return OCEANIC_ERROR_PROTOCOL; + } + + return OCEANIC_SUCCESS; +} + + int oceanic_atom2_read_version (atom2 *device, unsigned char data[], unsigned int size) { diff --git a/src/oceanic_atom2.h b/src/oceanic_atom2.h index 3749df4..bdb89d4 100644 --- a/src/oceanic_atom2.h +++ b/src/oceanic_atom2.h @@ -14,6 +14,10 @@ int oceanic_atom2_open (atom2 **device, const char* name); int oceanic_atom2_close (atom2 *device); +int oceanic_atom2_handshake (atom2 *device); + +int oceanic_atom2_quit (atom2 *device); + int oceanic_atom2_read_version (atom2 *device, unsigned char data[], unsigned int size); int oceanic_atom2_read_memory (atom2 *device, unsigned int address, unsigned char data[], unsigned int size);