From 9588f7db011c4459c6e562e18e30afc7bf578ae9 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Sat, 25 Dec 2010 21:44:31 +0100 Subject: [PATCH] Send the version command at startup. --- src/mares_iconhd.c | 48 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/mares_iconhd.c b/src/mares_iconhd.c index 7bb9959..d63422f 100644 --- a/src/mares_iconhd.c +++ b/src/mares_iconhd.c @@ -40,6 +40,9 @@ #define BAUDRATE 230400 #endif +#define ACK 0xAA +#define EOF 0xEA + #define RB_PROFILE_BEGIN 0xA000 #define RB_PROFILE_END MARES_ICONHD_MEMORY_SIZE @@ -75,6 +78,35 @@ device_is_mares_iconhd (device_t *abstract) } +static device_status_t +mares_iconhd_version (mares_iconhd_device_t *device) +{ + // Send the command to the dive computer. + unsigned char command[2] = {0xC2, 0x67}; + int n = serial_write (device->port, command, sizeof (command)); + if (n != sizeof (command)) { + WARNING ("Failed to send the command."); + return EXITCODE (n); + } + + // Receive the answer of the dive computer. + unsigned char answer[142] = {0}; + n = serial_read (device->port, answer, sizeof (answer)); + if (n != sizeof (answer)) { + WARNING ("Failed to receive the answer."); + return EXITCODE (n); + } + + // Verify the first and last byte. + if (answer[0] != ACK || answer[sizeof (answer) - 1] != EOF) { + WARNING ("Unexpected answer byte."); + return DEVICE_STATUS_PROTOCOL; + } + + return DEVICE_STATUS_SUCCESS; +} + + device_status_t mares_iconhd_device_open (device_t **out, const char* name) { @@ -132,6 +164,14 @@ mares_iconhd_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 version command. + device_status_t status = mares_iconhd_version (device); + if (status != DEVICE_STATUS_SUCCESS) { + serial_close (device->port); + free (device); + return status; + } + *out = (device_t *) device; return DEVICE_STATUS_SUCCESS; @@ -195,6 +235,12 @@ mares_iconhd_init (mares_iconhd_device_t *device) return EXITCODE (n); } + // Verify the first byte. + if (answer[0] != ACK) { + WARNING ("Unexpected answer byte."); + return DEVICE_STATUS_PROTOCOL; + } + return DEVICE_STATUS_SUCCESS; } @@ -268,7 +314,7 @@ mares_iconhd_device_dump (device_t *abstract, dc_buffer_t *buffer) } // Verify the last byte. - if (answer[0] != 0xEA) { + if (answer[0] != EOF) { WARNING ("Unexpected answer byte."); return DEVICE_STATUS_PROTOCOL; }